测试步骤:
1. 创建测试数据,以用户表为例,创建语句和添加数据语句如下:
/*创建表*/
[ID] [int] IDENTITY(1,1) NOT NULL,
[UserID] [varchar](20) COLLATE Chinese_PRC_CI_AS NULL,
[UserName] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[Address] [varchar](260) COLLATE Chinese_PRC_CI_AS NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
-----------------插入测试数据
INSERT [Users] ( [UserID],[UserName],[Address]) VALUES ('su20090812001','kevin','beijing')
INSERT [Users] ( [UserID],[UserName],[Address]) VALUES ( 'su20090912002','zhangsan','tianjin')
INSERT [Users] ( [UserID],[UserName],[Address]) VALUES ( 'su20090701234','wangwu','shanghai')
INSERT [Users] ( [UserID],[UserName],[Address]) VALUES ( 'su20090912004','lisi','beijing')
INSERT [Users] ( [UserID],[UserName],[Address]) VALUES ( 'su20090912005','jialiu','hebei')
INSERT [Users] ( [UserID],[UserName],[Address]) VALUES ( 'su20090912007','qianlong','zhejiang')
2.
在sql server management studio 中温习下 sql 查询加for xml后的结果集,主要是参考Using the FOR XML Clause to Return Query Results as XML文内容,在sql 语句结束处加for xml auto root(‘xxx’) elements等等。
(1):sql 语句:SELECT * FROM Users u FOR XML raw
执行结果如下:
<row ID="2" UserID="su20090912002" UserName="zhangsan" Address="tianjin"/>
<row ID="3" UserID="su20090701234" UserName="wangwu" Address="shanghai"/>
<row ID="4" UserID="su20090912004" UserName="lisi" Address="beijing"/>
<row ID="5" UserID="su20090912005" UserName="jialiu" Address="hebei"/>
<row ID="6" UserID="su20090912007" UserName="qianlong" Address="zhejiang"/>
(2):Sql语句:SELECT*FROM Users u FOR XML RAW('User')
<User ID="2" UserID="su20090912002" UserName="zhangsan" Address="tianjin"/>
<User ID="3" UserID="su20090701234" UserName="wangwu" Address="shanghai"/>
<User ID="4" UserID="su20090912004" UserName="lisi" Address="beijing"/>
<User ID="5" UserID="su20090912005" UserName="jialiu" Address="hebei"/>
<User ID="6" UserID="su20090912007" UserName="qianlong" Address="zhejiang" />
(3):Sql 语句SELECT * FROM Users u FOR XML RAW('User'),ROOT('Users'),elements 不在copy 执行结果了,大家自己执行下就可以看到效果了。
3. 在ASP.NET中如何获取这种形式的数据集呢。主要用到xmlreader(xmlTextReader),这里以xmlreader为例,
在程序代码中,主要用是用SqlCommand对象ExecuteXmlReader()方法,
下面是
具体的实现代码:
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
public partial class xmlUser : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetUserXml();
}
}
private void GetUserXml()
{
SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=kevin;database=demo;");
SqlCommand cmd = new SqlCommand("SELECT * FROM Users u FOR XML RAW('User'),ROOT('Users'),elements",conn);
XmlReader myreader = null;
try
{
conn.Open();
myreader = cmd.ExecuteXmlReader();
StringBuilder sb = new StringBuilder();
myreader.MoveToElement();
while (myreader.IsStartElement())
{ ///读取xml部分
sb.Append(myreader.ReadOuterXml());
}
///显示xml的内容
Response.Write(Server.HtmlEncode(sb.ToString()).Replace("\n", "<br>"));
}
catch(SqlException err)
{
//写日志略
}
finally
{
conn.Close();
}
}
}
运行结果:
<Users>
<User><ID>1</ID><UserID>su20090812001</UserID><UserName>kevin</UserName><Address>beijing</Address></User>
<User><ID>2</ID><UserID>su20090912002</UserID><UserName>zhangsan</UserName><Address>tianjin</Address></User>
<User><ID>3</ID><UserID>su20090701234</UserID><UserName>wangwu</UserName><Address>shanghai</Address></User>
<User><ID>4</ID><UserID>su20090912004</UserID><UserName>lisi</UserName><Address>beijing</Address></User>
<User><ID>5</ID><UserID>su20090912005</UserID><UserName>jialiu</UserName><Address>hebei</Address></User>
<User><ID>6</ID><UserID>su20090912007</UserID><UserName>qianlong</UserName><Address>zhejiang</Address></User>
</Users>
得到sql 返回的xml格式的数据,以前我的那个项目拼接成的xml格式的字符串就可以省去一大部分拼接代码。没有测试过是在程序这边拼接效率好还是数据库返回xml
数据效率好,但编写的程序代码是减少很多。有兴趣的同学帮忙测测这两种方式哪种效率更好些呢。欢迎大家扔砖头。
参考:
Using the FOR XML Clause to Return Query Results as XML
http://www.simple-talk.com/content/article.aspx?article=720
如何使用SQLSERVER2000中的XML功能
http://www.legalsoft.com.cn/docs/docs/11/1001.html
使用ExecuteXMLReader()读取XML片段