.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.





































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
Public
Class Sample
Public Shared Sub Main()
Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;Initial Catalog=northwind;Integrated Security=SSPI")
nwindConn.Open()

Dim myDataSet As DataSet = New DataSet("CustomerOrders")

Dim custDA As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Customers", nwindConn)
custDA.Fill(myDataSet, "Customers")

Dim ordersDA As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Orders", nwindConn)
ordersDA.Fill(myDataSet, "Orders")

nwindConn.Close()

myDataSet.Relations.Add("CustOrders", _
myDataSet.Tables("Customers").Columns("CustomerID"), _
myDataSet.Tables("Orders").Columns("CustomerID")).Nested = true

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)

xslTran.Transform(xmlDoc, Nothing, writer)
writer.Close()
End Sub
End Class
[C#]
using System;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Xml.Xsl;
public
class Sample
{
public static void Main()
{
SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Initial Catalog=northwind;Integrated Security=SSPI;");
nwindConn.Open();

DataSet custDS = new DataSet("CustomerDataSet");

SqlDataAdapter custDA = new SqlDataAdapter("SELECT * FROM Customers", nwindConn);
custDA.Fill(custDS, "Customers");

SqlDataAdapter ordersDA = new SqlDataAdapter("SELECT * FROM Orders", nwindConn);
ordersDA.Fill(custDS, "Orders");

nwindConn.Close();

custDS.Relations.Add("CustOrders",
custDS.Tables["Customers"].Columns["CustomerID"],
custDS.Tables["Orders"].Columns["CustomerID"]).Nested = true;

XmlDataDocument xmlDoc = new XmlDataDocument(custDS);
XslTransform xslTran = new XslTransform();
xslTran.Load("transform.xsl");
XmlTextWriter writer = new XmlTextWriter("xslt_output.html", System.Text.Encoding.UTF8);

xslTran.Transform(xmlDoc, null, writer);
writer.Close();
}
}
本文转自斯克迪亚博客园博客,原文链接:http://www.cnblogs.com/sgsoft/archive/2004/08/25/36309.html,如需转载请自行联系原作者