Danh mục

Transforming a DataSet Using XSLT

Số trang: 4      Loại file: pdf      Dung lượng: 16.53 KB      Lượt xem: 18      Lượt tải: 0    
Jamona

Hỗ trợ phí lưu trữ khi tải xuống: 2,000 VND Tải xuống file đầy đủ (4 trang) 0

Báo xấu

Xem trước 2 trang đầu tiên của tài liệu này:

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.
Nội dung trích xuất từ tài liệu:
Transforming a DataSet Using XSLT [ Team LiB ]Recipe 8.7 Transforming a DataSet Using XSLTProblemYou need to use an XSLT stylesheet to transform the contents of a DataSet.SolutionCreate an XslTransform object and call the Transform( ) method.Youll need to add the Microsoft Web Browser control to the Toolbox from the COM tabin the Customize Toolbox Dialog. Youll also need a reference to the Microsoft HTMLObject Library from the COM tab in Visual Studio .NETs 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 constantsusing System;using System.Configuration;using System.IO;using System.Xml;using System.Xml.Xsl;using System.Data;using System.Data.SqlClient;// Table name constantsprivate 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( );}DiscussionExtensible Stylesheet Transformations (XSLT) evolved from the Extensible StylesheetLanguage (XSL). XSLT defines a standard for XML data transformation—parsing aninput XML document and converting it into a result XML document. One common usefor XSLT is to transform XML data returned from a middle tier component into one ormore result documents (often HTML) to support different user interface or devicerequirements. For more information about XSLT, see Microsofts MSDN Library.In .NET, the DataSet is synchronized with the XmlDataDocument. As a result, in somecases XML services can be used to access the XmlDataDocument to perform certainfunctionality more conveniently than could be accomplished using the DataSet directly.To use XSLT to transform the contents of a DataSet, create an XslTransform object andcall 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 usingoverloaded versions of the Transform( ) method.[ Team LiB ]

Tài liệu được xem nhiều: