Sunday, January 20, 2013

The Xml Web Control in ASP.NET

ASP.NET includes an Xml web control that fills the gap and can display XML content. You can specify the XML content for this control in several ways: by assigning an XmlDocument object to the Document property, by assigning a string containing the XML content to the DocumentContent property, or by specifying a string that refers to an XML file using the DocumentSource property.

// Display the information from an XML file in the Xml control.
Xml.DocumentSource = Path.Combine(Request.PhysicalApplicationPath,

//XML File
<?xml version="1.0"?>
<Product ID="1" Name="Chair">
<Product ID="2" Name="Car">
<Product ID="3" Name="Fresh Fruit Basket">

If you assign the SuperProProductList.xml file to the Xml control, you’re likely to be disappointed. The result is just a string of the inner text (the price for each product), bunched together without a space (see Figure 1).

Figure 1

However, you can also apply an XSLT style sheet, either by assigning an XslCompiledTransform object to the Transform property or by using a string that refers to the XSLT file with the TransformSource property:

//XSLT sheet(Style sheet)
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl=""
version="1.0" >
<xsl:template match="SuperProProductList">
<table border="1">
<xsl:apply-templates select="Product"/>
<xsl:template match="Product">
<td><xsl:value-of select="@ID"/></td>
<td><xsl:value-of select="@Name"/></td>
<td><xsl:value-of select="Price"/></td>

// Specify a XSLT file.
Xml.TransformSource = Path.Combine(Request.PhysicalApplicationPath,

Now the output is automatically formatted according to your style sheet (see Figure 2).

Figure 2

No comments:
Write comments
Recommended Posts × +