FOR XML PATH¾ÙÀý
´ó¼Ò¶¼ÖªµÀÔÚSQL ServerÖÐÀûÓà FOR XML PATH Óï¾äÄܹ»°Ñ²éѯµÄÊý¾ÝÉú³ÉXMLÊý¾Ý£¬ÏÂÃæÊÇËüµÄһЩӦÓÃʾÀý¡£
DECLARE @TempTable table(UserID int , UserName nvarchar(50));
insert into @TempTable (UserID,UserName) values (1,'a')
insert into @TempTable (UserID,UserName) values (2,'b')
select UserID,UserName from @TempTable FOR XML PATH
ÔËÐÐÕâ¶Î½Å±¾£¬½«Éú³ÉÈçϽá¹û£º
<row>
<UserID>1</UserID>
<UserName>a</UserName>
</row>
<row>
<UserID>2</UserID>
<UserName>b</UserName>
</row>
´ó¼Ò¿ÉÒÔ¿´µ½Á½ÐÐÊý¾ÝÉú³ÉÁËÁ½¸ö½Úµã£¬ÐÞ¸ÄÒ»ÏÂPATHµÄ²ÎÊý£º
select UserID,UserName from @TempTable FOR XML PATH('lzy')
ÔÙ´ÎÔËÐÐÉÏÊö½Å±¾£¬½«Éú³ÉÈçϵĽá¹û£º
<lzy>
<UserID>1</UserID>
<UserName>a</UserName>
</lzy>
<lzy>
<UserID>2</UserID>
<UserName>b</UserName>
</lzy>
¿ÉÒÔ¿´µ½½Úµã±ä³É£¬ÆäʵPATH() À¨ºÅÄڵIJÎÊýÊÇ¿ØÖƽڵãÃû³ÆµÄ£¬ÕâÑùµÄ»°´ó¼Ò¿ÉÒÔ¿´Ò»ÏÂÈç¹ûÊÇ¿Õ×Ö·û´®£¨²»ÊÇûÓвÎÊý£©»áÊÇʲô½á¹û?
select UserID,UserName from @TempTable FOR XML PATH('')
Ö´ÐÐÉÏÃæÕâ¶Î½Å±¾½«Éú³É½á¹û£º
<UserID>1</UserID>
<UserName>a</UserName>
<UserID>2</UserID>
<UserName>b</UserName>
ÕâÑù¾Í²»ÏÔʾÉϼ¶½ÚµãÁË£¬´ó¼ÒÖªµÀÔÚ PATH ģʽÖУ¬ÁÐÃû»òÁбðÃû±»×÷Ϊ XPath ±í´ïʽÀ´´¦Àí£¬Ò²¾ÍÊÇ˵£¬ÊÇÁеÄÃû×Ö£¬ÕâÑù´óµ¨ÊÔÑéһϲ»¸øÖ¸¶¨ÁÐÃûºÍ±ðÃû»áÊÇÔõôÑù£¿
select CAST(UserID AS varchar) + '',UserName + '' from @TempTable FOR XML PATH('')
ÔËÐÐÉÏÃæÕâ¾ä½«Éú³É½á¹û
1a2b
ËùÓÐÊý¾Ý¶¼Éú³ÉÒ»ÐУ¬¶øÇÒ»¹Ã»ÓÐÁ¬½Ó×Ö·û£¬ÕâÑùµÄÊý¾Ý¿ÉÄܶԴó¼ÒûÓÐÓô¦£¬»¹¿ÉÒÔÔٱ仯һÏ£º
select CAST(UserID AS varchar) + ',',UserName + '',';' from @TempTable FOR XML PATH('')
Éú³É½á¹û
1,a;2,b;
´ó¼ÒÏÖÔÚÃ÷°×ÁË°É£¬¿ÉÒÔͨ¹ý¿ØÖƲÎÊýÀ´Éú³É×Ô¼ºÏëÒªµÄ½á¹û£¬ÀýÈ磺
select '{' + CAST(UserID AS varchar) + ',','"' +UserName + '"','}' from @TempTable FOR XML PATH('')
Éú³É½á¹û
{1,"a"}{2,"b"}
»¹¿ÉÒÔÉú³ÉÆäËû¸ñʽ£¬´ó¼Ò¿ÉÒ
Ïà¹ØÎĵµ£º
Input.xml-----------
<?xml version="1.0" encoding="gb2312"?>
<studentList>
<student sex="man">
<name>wgy</name>
<age>23</age>
<tel>12345678</tel>
</student>
<student sex="female">
<name>lemon</name&g ......
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Xml;
using System.Data;
public class Cls_XML
{
#region ´´½¨xmlÎļþ
/// <summary>
/// ´´½¨xmlÎļþ
/// ......
HTML £ Hyper Text Mark-up Language £ ³¬Îı¾±ê¼ÇÓïÑÔ
HTML-³¬Îı¾±ê¼ÇÓïÑÔ£¬ÊÇWWWµÄÃèÊöÓïÑÔ¡£Éè¼ÆHTMLÓïÑÔµÄÄ¿µÄÊÇΪÁËÄÜ°Ñ´æ·ÅÔÚһ̨µçÄÔÖеÄÎı¾»òͼÐÎÓëÁíһ̨µçÄÔÖеÄÎı¾»òͼÐη½±ãµØÁªÏµÔÚÒ»Æð£¬ÐγÉÓлúµÄÕûÌ壬ÈËÃDz»Óÿ¼ÂǾßÌåÐÅÏ¢ÊÇÔÚµ±Ç°µçÄÔÉÏ»¹ÊÇÔÚÍøÂçµÄÆäËüµçÄÔÉÏ¡£ÎÒÃÇÖ» ......