Using the ASP.NET Xml Control


Using the ASP.NET Xml Control

You can use the ASP.NET Xml control to simply display the raw contents of an XML document. Alternatively, you can use the control with an XSL stylesheet to format an XML document.

The Xml control supports the following four properties:

  • DocumentSource ” The path to an XML file

  • TransformSource ” The path to an XSL file

  • Document ” A preloaded XML document

  • Transform ” A preloaded XSL stylesheet

If you want to grab an XML file from the file system and transform the XML data with an XSL stylesheet, you can use the DocumentSource and TransformSource properties. For example, the page in Listing 13.13 uses the Xml control to format the contents of the Recipes.xml file (see Figure 13.6).

Listing 13.13 XmlControl.aspx
 <html> <head><title>XMLControl.aspx</title></head> <body> <asp:Xml   DocumentSource="recipes.xml"   TransformSource="TransformRecipes.xsl"   Runat="Server" /> </body> </html> 

The C# version of this code can be found on the CD-ROM.

Figure 13.6. Displaying an XML document with the Xml control.

graphics/13fig06.jpg

The Xml control in Listing 13.13 uses an XSL stylesheet named TransformRecipes.xsl to format the contents of the recipes.xml file. The TransformRecipes.xsl file is contained in Listing 13.14.

Listing 13.14 TransformRecipes.xsl
 <xsl:stylesheet version="1.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   <xsl:template match="Recipe">     <h2><xsl:value-of select="Name" /></h2>     <xsl:for-each select="Ingredients/Ingredient" >       <li><xsl:value-of select="." /></li>     </xsl:for-each>   </xsl:template> </xsl:stylesheet> 

The C# version of this code can be found on the CD-ROM.

In Listing 13.14, an XSL stylesheet uses XPath expressions to match elements in an XML file. When an element is matched, a template indicates how the element should be formatted.

NOTE

To learn more details about XPath , visit the W3C Web site at http://www.w3.org/tr/xpath.


In Listing 13.14, the first template matches Recipe elements. Whenever a Recipe element is found, the Name element is formatted with the HTML <H2> tag. Next, each ingredient for a particular recipe is formatted with HTML <LI> tags to create a bulleted list.

You can use the Xml control's Document and Transform properties with preloaded XML documents and XSL stylesheets. For example, the page in Listing 13.15 uses the Xml control to format the contents of a DataSet (see Figure 13.7).

Listing 13.15 DataSetTransform.aspx
 <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <%@ Import Namespace="System.Xml" %> <Script Runat="Server"> Sub Page_Load   Dim conNorthwind As SqlConnection   Dim dadProducts As SqlDataAdapter   Dim dstProducts As DataSet   conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=Northwind" )   dadProducts = New SqlDataAdapter( "Select * From Products", conNorthwind )   dstProducts = New DataSet()   dadProducts.Fill( dstProducts, "Products" )   xmlProducts.Document = New XmlDataDocument( dstProducts )   xmlProducts.TransformSource = "TransformProducts.xsl" End Sub </Script> <html> <head><title>DataSetTransform.aspx</title></head> <body> <asp:Xml   ID="xmlProducts"   Runat="Server" /> </body> </html> 

The C# version of this code can be found on the CD-ROM.

Figure 13.7. Transformed XML document.

graphics/13fig07.jpg

In Listing 13.15, a DataSet is populated with the contents of the Products table from the Northwind database. An XmlDataDocument is created from the DataSet and assigned to the Document property of the Xml control. The Xml control uses an XSL stylesheet named TransformProducts.xsl to format the list of products in the DataSet .

The TransformProducts.xsl stylesheet is included in Listing 13.16.

Listing 13.16 TransformProducts.xsl
 <xsl:stylesheet version="1.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   <xsl:template match="Products">     <p>     <table width="100%" border="1"       cellpadding="10" cellspacing="0">       <tr>         <td bgcolor="yellow">           <b><xsl:value-of select="ProductName" /> </b>         </td>       </tr>       <tr bgcolor="#eeeeee">         <td>           <b>Price:</b>           $<xsl:value-of select="UnitPrice" />         </td>       </tr>     </table>     </p>   </xsl:template> </xsl:stylesheet> 

The C# version of this code can be found on the CD-ROM.

The stylesheet in Listing 13.16 formats the products from the Products database table. It contains one template that matches all the Products elements. Each product is displayed as a separate HTML table. Within the HTML table, both the name and price of the product are displayed.



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

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