Tài liệu Transforming a DataSet Using XSLT ppt

4 257 0
Tài liệu Transforming a DataSet Using XSLT ppt

Đang tải... (xem toàn văn)

Thông tin tài liệu

[ Team LiB ] Recipe 8.7 Transforming a DataSet Using XSLT Problem You need to use an XSLT stylesheet to transform the contents of a DataSet. Solution Create an XslTransform object and call the Transform( ) method. You'll need to add the Microsoft Web Browser control to the Toolbox from the COM tab in the Customize Toolbox Dialog. You'll also need a reference to the Microsoft HTML Object Library from the COM tab in Visual Studio .NET's Add Reference Dialog. Example 8-10 uses one XML file: Category.xslt The XSLT stylesheet used to transform the XML for the Categories table in the DataSet into HTML displaying the Categories data in an HTML table. The contents of this XML file are shown in Example 8-10 . The sample code contains three event handlers: Form.Load Sets up the sample by filling a DataSet with the Categories table from Northwind. Transform Button.Click Initializes a WebBrowser control. Once the control is initialized, the DocumentComplete event is raised. The handler for the DocumentComplete event completes the XSLT transformation. DocumentComplete Gets the XmlDataDocument for the DataSet, creates an XSLTransform class, and transforms the XML document using the XSLT stylesheet. The results are displayed both in the WebBrowser control and as XML. Example 8-10. File: Category.xslt <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet xmlns:xsl=http://www.w3.org/1999/XSL/Transform version="1.0"> <xsl:template match="/"> <html> <body> <table> <tr bgcolor="#AAAAAA"> <td>Category ID</td> <td>Name</td> <td>Description</td> </tr> <xsl:for-each select="/CategoriesDS/Categories"> <tr> <td> <xsl:value-of select="CategoryID" /> </td> <td> <xsl:value-of select="CategoryName" /> </td> <td> <xsl:value-of select="Description" /> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> The C# code is shown in Example 8-11 . Example 8-11. File: XslTransformForm.cs // Namespaces, variables, and constants using System; using System.Configuration; using System.IO; using System.Xml; using System.Xml.Xsl; using System.Data; using System.Data.SqlClient; // Table name constants private const String CATEGORIES_TABLE = "Categories"; private const String XSLTFILENAME = ConfigurationSettings.AppSettings["Project_Directory"] + @"Chapter 08\Category.xslt"; private DataSet ds; // . . . private void XslTransformForm_Load(object sender, System.EventArgs e) { // Fill the Categories within a DataSet. SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Categories", ConfigurationSettings.AppSettings["Sql_ConnectString"]); ds = new DataSet("CategoriesDS"); da.Fill(ds, CATEGORIES_TABLE); } private void transformButton_Click(object sender, System.EventArgs e) { // Create parameters to create web browser. String url = "about:blank"; object flags = 0; object targetFrameName = String.Empty; object postData = String.Empty; object headers = String.Empty; // Must wait for the navigation to complete so use the // DocumentComplete event for the rest of the processing webBrowser.Navigate(url, ref flags, ref targetFrameName, ref postData, ref headers); } private void webBrowser_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e) { // Apply the XML transformation storing results to StringWriter. XslTransform xslt = new XslTransform( ); xslt.Load(XSLTFILENAME); StringWriter sw = new StringWriter( ); xslt.Transform(new XmlDataDocument(ds), null, sw, (XmlResolver)null); // Load the results of the transformation into the web browser. mshtml.IHTMLDocument2 htmlDoc = (mshtml.IHTMLDocument2)webBrowser.Document; htmlDoc.body.innerHTML = sw.ToString( ); // Display the results of the transformation. resultTextBox.Text = sw.ToString( ); } Discussion Extensible Stylesheet Transformations (XSLT) evolved from the Extensible Stylesheet Language (XSL). XSLT defines a standard for XML data transformation—parsing an input XML document and converting it into a result XML document. One common use for XSLT is to transform XML data returned from a middle tier component into one or more result documents (often HTML) to support different user interface or device requirements. For more information about XSLT, see Microsoft's MSDN Library. In .NET, the DataSet is synchronized with the XmlDataDocument. As a result, in some cases XML services can be used to access the XmlDataDocument to perform certain functionality more conveniently than could be accomplished using the DataSet directly. To use XSLT to transform the contents of a DataSet, create an XslTransform object and call the Transform( ) method on that object, as shown in this example: XslTransform xslt = new XslTransform( ); xslt.Load(XSLTFILENAME); StringWriter sw = new StringWriter( ); xslt.Transform(new XmlDataDocument(ds), null, sw, (XmlResolver)null); The results of the transformation can be sent to a variety of output formats using overloaded versions of the Transform( ) method. [ Team LiB ] . private void XslTransformForm_Load(object sender, System.EventArgs e) { // Fill the Categories within a DataSet. SqlDataAdapter da = new SqlDataAdapter("SELECT. [ Team LiB ] Recipe 8.7 Transforming a DataSet Using XSLT Problem You need to use an XSLT stylesheet to transform the contents of a DataSet. Solution

Ngày đăng: 21/01/2014, 11:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan