intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Transforming a DataSet Using XSLT

Chia sẻ: Bui Tuan | Ngày: | Loại File: PDF | Số trang:4

93
lượt xem
10
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

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

Chủ đề:
Lưu

Nội dung Text: Transforming a DataSet Using XSLT

  1. [ 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
  2. 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;
  3. 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);
  4. 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 ]
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2