以上结果保证符合XML数据类型提供的格式良好的约束。由于结果是XML数据类型实例,所以还可以使用XQuery表达式查询和整理结果。例如,以下表达式将获取的员工头衔添加到新元素中:
SELECT (SELECT * FROM HumanResources.Employee as Employee上面的查询将生成如下输出:
FOR XML AUTO, TYPE).query(
'<Output>{
for $c in /Employee
return <Employee name="{data($c/@Title)} "/>
}</Output>')
<Output>由于FOR XML查询返回可赋予的值,所以FOR XML查询结果能够赋值给XML变量或者插入XML列中:
<Employee name="Production Technician - WC60" />
<Employee name="Marketing Assistant" />
<Employee name="Engineering Manager" />
------
------
</Output>
/* 将FOR XML输出赋值给变量 */在这些语句中,开发人员将FOR XML查询结果赋值给一个XML数据类型变量,然后利用该变量将值插入Employee_New表中。
DECLARE @Employee XML;
SET @Employee = (SELECT * FROM HumanResources.Employee FOR XML AUTO, TYPE)
CREATE TABLE Employee_New (EmployeeID int, XmlData XML)
/* 将FOR XML输出赋值给列 */
INSERT INTO Employee_New SELECT 1, @Employee
2. 利用ADO.NET执行FOR XML查询
为了通过FOR XML查询直接从SQL Server返回XML流,需要使用SqlCommand对象的ExecuteXmlReader()方法。ExecuteXmlReader()方法可返回SqlCommand设置的查询结果生成的XmlReader对象。示例1说明了使用ExecuteXmlReader()方法查询AdventureWorks数据库的DatabaseLog表。
示例1:使用ExecuteXmlReader方法执行FOR XML查询
<%@ Page Language="C#" ValidateRequest="false" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.Sql" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Web.Configuration" %>
<%@ Import Namespace="System.Data.SqlTypes" %>
<script runat="server">
void btnReadXml_Click(object sender, EventArgs e)
{
int ID = Convert.ToInt32(txtID.Text);
// 从web.config文件中获取连接字符串
string connectionString = WebConfigurationManager.ConnectionStrings["AdventureWorks"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
System.Text.StringBuilder builder = new System.Text.StringBuilder();
connection.Open();
SqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT DatabaseLogID, XmlEvent FROM " +
" DatabaseLog WHERE DatabaseLogID = " + ID.ToString() +
" FOR XML AUTO, ROOT('DatabaseLogs'), ELEMENTS";
XmlReader reader = command.ExecuteXmlReader();
XmlDocument doc = new XmlDocument();
// 将XmlReader加载到XmlDocument对象中
doc.Load(reader);
builder.Append("<b>Complete XML :</b>" + Server.HtmlEncode(doc.OuterXml) + "<br><br>");
// 获取DatabaseLogID和XmlEvent列的值
string idValue = doc.DocumentElement.SelectSingleNode
("DatabaseLog/DatabaseLogID").InnerText;
builder.Append("<b>id :</b>" + Server.HtmlEncode(idValue) +
"<br><br>");
string xmlEventValue = doc.DocumentElement.SelectSingleNode("DatabaseLog/XmlEvent").OuterXml;
builder.Append("<b>XmlEvent :</b>" + Server.HtmlEncode(xmlEventValue) + "<br>");
output.Text = builder.ToString();
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Executing a FOR XML Query from ADO.NET</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblID" runat="server" Text="Enter ID:"></asp:Label>
<asp:TextBox ID="txtID" runat="server"></asp:TextBox>
<asp:Button ID="btnReadXml" runat="server" Text="Read Xml"
Width="118px" Height="30px" OnClick="btnReadXml_Click" />
<br />
<br />
<br />
<asp:Literal runat="server" ID="output" />
</div>
</form>
</body>
</html>