Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

XML Data-Binding Powers Up in ASP.NET 2.0 : Page 3

In ASP.NET 2.0, the new XmlDataSource control greatly simplifies binding XML sources to controls, letting you display hierarchical XML data easily. It even leverages XSLT, XPath, and caching, eliminating much manual coding.


advertisement
Performing XSL Transformations with the XmlDataSource Control
You can also transform the XML data before displaying it in a data-bound control by providing an Extensible Stylesheet Language (XSL) stylesheet for the transformation. You typically use the TransformFile property, which accepts a file name, to load the stylesheet from a file, but you can also load a stylesheet directly from a string using the Transform property.

Here's an example XSL stylesheet that demonstrates how to apply an XSL Stylesheet file to the contents of the XmlDataSource control to transform that XML format into a different XML form before displaying the data in a TreeView control.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <products-list> <xsl:for-each select="//Products/Product"> <product> <xsl:attribute name="id"> <xsl:value-of select="@ID"/> </xsl:attribute> <xsl:attribute name="name"> <xsl:value-of select="@Name"/> </xsl:attribute> <xsl:attribute name="price"> <xsl:value-of select="@Price"/> </xsl:attribute> <xsl:element name="desc"> <xsl:attribute name="value"> <xsl:value-of select="Desc/@Value"/> </xsl:attribute> </xsl:element> </product> </xsl:for-each> </products-list> </xsl:template> </xsl:stylesheet>

The XSL file transforms the ProductDetails.xml file into a different format—changing the names of the XML elements and the case of the attributes in the output. The next example shows how to apply the ProductDetails.xsl file to the ProductDetails.xml file, using a TreeView control to display the transformed XML content.

<%@ Page Language="VB" %> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>XML Transformation</title> </head> <body> <form id="form1" runat="server"> <div> <asp:TreeView ID="TreeView1" Runat="server" DataSourceID="XmlDataSource1"> <DataBindings> <asp:TreeNodeBinding DataMember="product" ValueField="id" TextField="name"></asp:TreeNodeBinding> <asp:TreeNodeBinding DataMember="desc" ValueField="value" TextField="value"></asp:TreeNodeBinding> </DataBindings> </asp:TreeView> <asp:XmlDataSource ID="XmlDataSource1" Runat="server" DataFile="~/Data/ProductDetails.xml" TransformFile="~/Data/ProductDetails.xsl" /> </div> </form> </body> </html>

 
Figure 3. The Transformed XPath Example ASP.NET Page: The page shown in this figure was created by using an XSL Stylesheet file in conjunction with an XmlDataSource control to transform one XML format into another XML format, binding the result to the TreeView.
As part of the XmlDataSource control declaration, the DataFile and TransformFile attributes (properties) are set to ProductDetails.xml and ProductDetails.xsl respectively. After setting the attributes you can easily display the data in a TreeView control by setting the DataSourceID property of the TreeView control to the XmlDataSource control. Inside the TreeView declaration, you use the asp:TreeNodeBinding elements to specify which nodes and child nodes to display in the TreeView control.

Figure 3 shows the output produced by the page. Note that after the transformation, the top level node is products-list rather than Products as in the original ProductDetails.xml file. Apart from that, the data displayed is unchanged.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date