对DataSet应用XSLT转换
.NET Framework Developer's Guide |
The WriteXml method of the DataSet enables you to write the contents of a DataSet as XML data. A common task is to then transform that XML to another format using XSL Transformations (XSLT). However, synchronizing a DataSet with an XmlDataDocument enables you to apply an XSLT stylesheet to the contents of a DataSet without having to first write the contents of the DataSet as XML data using WriteXml.
The following example populates a DataSet with tables and relationships, synchronizes the DataSet with an XmlDataDocument, and writes a portion of the DataSet as an HTML file using an XSLT stylesheet. Following are the contents of the XSLT stylesheet.
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
The following code is the code to fill the DataSet and apply the XSLT style sheet.
Note If the DataSet that you are applying an XSLT style sheet to contains relations, you will achieve best performance if you set the Nested property of the DataRelation to true for each nested relation. This allows you to use XSLT style sheets that implement natural top-down processing to navigate the hierarchy and transform the data, as opposed to using performance-intensive XPath location axes (for example, preceding-sibling and following-sibling in style sheet node test expressions) to navigate the data hierarchy. For more information on nested relations, see Nested DataRelations.
[Visual Basic]
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Xml
Imports System.Xml.Xsl
![](/Images/OutliningIndicators/None.gif)
Public Class Sample
Public Shared Sub Main()
Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;Initial Catalog=northwind;Integrated Security=SSPI")
nwindConn.Open()
![](/Images/OutliningIndicators/InBlock.gif)
Dim myDataSet As DataSet = New DataSet("CustomerOrders")
![](/Images/OutliningIndicators/InBlock.gif)
Dim custDA As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Customers", nwindConn)
custDA.Fill(myDataSet, "Customers")
![](/Images/OutliningIndicators/InBlock.gif)
Dim ordersDA As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Orders", nwindConn)
ordersDA.Fill(myDataSet, "Orders")
![](/Images/OutliningIndicators/InBlock.gif)
nwindConn.Close()
![](/Images/OutliningIndicators/InBlock.gif)
myDataSet.Relations.Add("CustOrders", _
myDataSet.Tables("Customers").Columns("CustomerID"), _
myDataSet.Tables("Orders").Columns("CustomerID")).Nested = true
![](/Images/OutliningIndicators/InBlock.gif)
Dim xmlDoc As XmlDataDocument = New XmlDataDocument(myDataSet)
Dim xslTran As XslTransform = New XslTransform
xslTran.Load("transform.xsl")
Dim writer As XmlTextWriter = New XmlTextWriter("xslt_output.html", System.Text.Encoding.UTF8)
![](/Images/OutliningIndicators/InBlock.gif)
xslTran.Transform(xmlDoc, Nothing, writer)
writer.Close()
End Sub
End Class
![](/Images/OutliningIndicators/None.gif)
[C#]
using System;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Xml.Xsl;
![](/Images/OutliningIndicators/None.gif)
public class Sample
{
public static void Main()
{
SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Initial Catalog=northwind;Integrated Security=SSPI;");
nwindConn.Open();
![](/Images/OutliningIndicators/InBlock.gif)
DataSet custDS = new DataSet("CustomerDataSet");
![](/Images/OutliningIndicators/InBlock.gif)
SqlDataAdapter custDA = new SqlDataAdapter("SELECT * FROM Customers", nwindConn);
custDA.Fill(custDS, "Customers");
![](/Images/OutliningIndicators/InBlock.gif)
SqlDataAdapter ordersDA = new SqlDataAdapter("SELECT * FROM Orders", nwindConn);
ordersDA.Fill(custDS, "Orders");
![](/Images/OutliningIndicators/InBlock.gif)
nwindConn.Close();
![](/Images/OutliningIndicators/InBlock.gif)
custDS.Relations.Add("CustOrders",
custDS.Tables["Customers"].Columns["CustomerID"],
custDS.Tables["Orders"].Columns["CustomerID"]).Nested = true;
![](/Images/OutliningIndicators/InBlock.gif)
XmlDataDocument xmlDoc = new XmlDataDocument(custDS);
XslTransform xslTran = new XslTransform();
xslTran.Load("transform.xsl");
XmlTextWriter writer = new XmlTextWriter("xslt_output.html", System.Text.Encoding.UTF8);
![](/Images/OutliningIndicators/InBlock.gif)
xslTran.Transform(xmlDoc, null, writer);
writer.Close();
}
}