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



 
  Category ID   Name   Description  
 
             
 

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 08Category.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 transformationparsing 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.

Connecting to Data

Retrieving and Managing Data

Searching and Analyzing Data

Adding and Modifying Data

Copying and Transferring Data

Maintaining Database Integrity

Binding Data to .NET User Interfaces

Working with XML

Optimizing .NET Data Access

Enumerating and Maintaining Database Objects

Appendix A. Converting from C# to VB Syntax



ADO. NET Cookbook
ADO.NET 3.5 Cookbook (Cookbooks (OReilly))
ISBN: 0596101406
EAN: 2147483647
Year: 2002
Pages: 222
Authors: Bill Hamilton

Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net