DataSet 的 WriteXml 方法使您能够以 XML 数据形式来编写 DataSet 的内容。随后的一项常见任务是使用 XSL 转换 (XSLT) 将该 XML 转换为另一种格式。不过,通过使 DataSet 与 XmlDataDocument 同步,可以将 XSLT 样式表应用于 DataSet 的内容,而不必首先使用 WriteXml 以 XML 数据形式来编写 DataSet 的内容。
以下示例使用表和关系填充一个 DataSet,使该 DataSet 与 XmlDataDocument 同步,然后使用 XSLT 样式表以 HTML 文件形式来编写该 DataSet 的一部分。下面是该 XSLT 样式表的内容。
1
<
xsl:stylesheet
xmlns:xsl
="http://www.w3.org/1999/XSL/Transform"
version
="1.0"
>
2
3 < xsl:template match ="CustomerOrders" >
4 < HTML >
5 < STYLE >
6 BODY {font-family:verdana;font-size:9pt}
7 TD {font-size:8pt}
8 </ STYLE >
9 < BODY >
10 < TABLE BORDER ="1" >
11 < xsl:apply-templates select ="Customers" />
12 </ TABLE >
13 </ BODY >
14 </ HTML >
15 </ xsl:template >
16
17 < xsl:template match ="Customers" >
18 < TR >< TD >
19 < xsl:value-of select ="ContactName" /> , < xsl:value-of select ="Phone" />< BR />
20 </ TD ></ TR >
21 < xsl:apply-templates select ="Orders" />
22 </ xsl:template >
23
24 < xsl:template match ="Orders" >
25 < TABLE BORDER ="1" >
26 < TR >< TD valign ="top" >< B > Order: </ B ></ TD >< TD valign ="top" >< xsl:value-of select ="OrderID" /></ TD ></ TR >
27 < TR >< TD valign ="top" >< B > Date: </ B ></ TD >< TD valign ="top" >< xsl:value-of select ="OrderDate" /></ TD ></ TR >
28 < TR >< TD valign ="top" >< B > Ship To: </ B ></ TD >
29 < TD valign ="top" >< xsl:value-of select ="ShipName" />< BR />
30 < xsl:value-of select ="ShipAddress" />< BR />
31 < xsl:value-of select ="ShipCity" /> , < xsl:value-of select ="ShipRegion" /> < xsl:value-of select ="ShipPostalCode" />< BR />
32 < xsl:value-of select ="ShipCountry" /></ TD ></ TR >
33 </ TABLE >
34 </ xsl:template >
35
36 </ xsl:stylesheet >
37
2
3 < xsl:template match ="CustomerOrders" >
4 < HTML >
5 < STYLE >
6 BODY {font-family:verdana;font-size:9pt}
7 TD {font-size:8pt}
8 </ STYLE >
9 < BODY >
10 < TABLE BORDER ="1" >
11 < xsl:apply-templates select ="Customers" />
12 </ TABLE >
13 </ BODY >
14 </ HTML >
15 </ xsl:template >
16
17 < xsl:template match ="Customers" >
18 < TR >< TD >
19 < xsl:value-of select ="ContactName" /> , < xsl:value-of select ="Phone" />< BR />
20 </ TD ></ TR >
21 < xsl:apply-templates select ="Orders" />
22 </ xsl:template >
23
24 < xsl:template match ="Orders" >
25 < TABLE BORDER ="1" >
26 < TR >< TD valign ="top" >< B > Order: </ B ></ TD >< TD valign ="top" >< xsl:value-of select ="OrderID" /></ TD ></ TR >
27 < TR >< TD valign ="top" >< B > Date: </ B ></ TD >< TD valign ="top" >< xsl:value-of select ="OrderDate" /></ TD ></ TR >
28 < TR >< TD valign ="top" >< B > Ship To: </ B ></ TD >
29 < TD valign ="top" >< xsl:value-of select ="ShipName" />< BR />
30 < xsl:value-of select ="ShipAddress" />< BR />
31 < xsl:value-of select ="ShipCity" /> , < xsl:value-of select ="ShipRegion" /> < xsl:value-of select ="ShipPostalCode" />< BR />
32 < xsl:value-of select ="ShipCountry" /></ TD ></ TR >
33 </ TABLE >
34 </ xsl:template >
35
36 </ xsl:stylesheet >
37
以下代码是填充 DataSet 和应用 XSLT 样式表的代码。
注意 当应用 XSLT 样式表的 DataSet 包含关系时,如果为每个嵌套关系将 DataRelation 的 Nested 属性设置为 true,将获得最佳性能。此设置使您可以使用 XSLT 样式表,执行正常的由上而下处理以遍历层次结构和转换数据,而不是使用性能密集的 XPath 定位轴(例如,样式表节点测试表达式中的前同辈和后同辈)来遍历数据层次结构。有关嵌套关系的更多信息,请参见 嵌套的 DataRelation。
1 using System;
2 using System.Data;
3 using System.Data.SqlClient;
4 using System.Xml;
5 using System.Xml.Xsl;
6
7 public class Sample
8 {
9 public static void Main()
10 {
11 SqlConnection nwindConn = new SqlConnection( " Data Source=localhost;Initial Catalog=northwind;Integrated Security=SSPI; " );
12 nwindConn.Open();
13
14 DataSet custDS = new DataSet( " CustomerDataSet " );
15
16 SqlDataAdapter custDA = new SqlDataAdapter( " SELECT * FROM Customers " , nwindConn);
17 custDA.Fill(custDS, " Customers " );
18
19 SqlDataAdapter ordersDA = new SqlDataAdapter( " SELECT * FROM Orders " , nwindConn);
20 ordersDA.Fill(custDS, " Orders " );
21
22 nwindConn.Close();
23
24 custDS.Relations.Add( " CustOrders " ,
25 custDS.Tables[ " Customers " ].Columns[ " CustomerID " ],
26 custDS.Tables[ " Orders " ].Columns[ " CustomerID " ]).Nested = true ;
27
28 XmlDataDocument xmlDoc = new XmlDataDocument(custDS);
29
30 XslTransform xslTran = new XslTransform();
31 xslTran.Load( " transform.xsl " );
32
33 XmlTextWriter writer = new XmlTextWriter( " xslt_output.html " , System.Text.Encoding.UTF8);
34
35 xslTran.Transform(xmlDoc, null , writer);
36 writer.Close();
37 }
38 }
1
public
void
ProcessRequest(HttpContext context)
2 {
3 context.Response.ContentType = " text/xml " ;
4
5 int TypeId = Convert.ToInt32(context.Request[ " TypeId " ]);
6 int AID = Convert.ToInt32(context.Request[ " AID " ]);
7 int MemberId = 0 ;
8
9 if ( ! String.IsNullOrEmpty(SessionHelper.CurrentMemberId()))
10 {
11 MemberId = Convert.ToInt32(SessionHelper.CurrentMemberId());
12 }
13
14 object [] Params = { AID, TypeId, MemberId };
15 DataSet hotDs = SQLHelperV2.DataSetUseTrans( " F_Last_ArticleShow " , Params);
16
17 hotDs.Tables[ 0 ].TableName = " ArtListAll " ;
18
19 XmlDataDocument doc = new XmlDataDocument(hotDs);
20
21 XslCompiledTransform tran = new XslCompiledTransform();
22
23 XsltSettings settings = new XsltSettings( true , true );
24
25 tran.Load(context.Server.MapPath( " /ArtHander/HotNewsList.xsl " ), settings, null );
26
27 XsltArgumentList xslArg = new XsltArgumentList();
28 xslArg.AddParam( " TypeId " , "" , context.Request[ " TypeId " ]);
29
30 StringBuilder sb = new StringBuilder();
31 StringWriter sw = new StringWriter(sb);
32
33 XmlTextWriter writer = new XmlTextWriter(sw);
34
35 tran.Transform(doc, xslArg, writer);
36
37 writer.Close();
38
39 context.Response.Write(sb.ToString());
40
41 }
2 {
3 context.Response.ContentType = " text/xml " ;
4
5 int TypeId = Convert.ToInt32(context.Request[ " TypeId " ]);
6 int AID = Convert.ToInt32(context.Request[ " AID " ]);
7 int MemberId = 0 ;
8
9 if ( ! String.IsNullOrEmpty(SessionHelper.CurrentMemberId()))
10 {
11 MemberId = Convert.ToInt32(SessionHelper.CurrentMemberId());
12 }
13
14 object [] Params = { AID, TypeId, MemberId };
15 DataSet hotDs = SQLHelperV2.DataSetUseTrans( " F_Last_ArticleShow " , Params);
16
17 hotDs.Tables[ 0 ].TableName = " ArtListAll " ;
18
19 XmlDataDocument doc = new XmlDataDocument(hotDs);
20
21 XslCompiledTransform tran = new XslCompiledTransform();
22
23 XsltSettings settings = new XsltSettings( true , true );
24
25 tran.Load(context.Server.MapPath( " /ArtHander/HotNewsList.xsl " ), settings, null );
26
27 XsltArgumentList xslArg = new XsltArgumentList();
28 xslArg.AddParam( " TypeId " , "" , context.Request[ " TypeId " ]);
29
30 StringBuilder sb = new StringBuilder();
31 StringWriter sw = new StringWriter(sb);
32
33 XmlTextWriter writer = new XmlTextWriter(sw);
34
35 tran.Transform(doc, xslArg, writer);
36
37 writer.Close();
38
39 context.Response.Write(sb.ToString());
40
41 }