C# .NET - Display table data from a dynamically generated xml in an aspx page, using xsl.

Asked By Rakesh Vikram on 28-Jan-10 07:41 AM


I have successfully formed an xml dynamically, in code behind. Now, I need to display the xml in a table using the xsl. I want to know the transformation process. (Displaying the xml data using xsl).

I tried to implement it using Java Script & Asp.net code behind, but unable to get the desired output.

Please help me. Thanks in advance.

Rakesh Vikram

How about binding the xml to a Data control? - [)ia6l0 iii replied to Rakesh Vikram on 28-Jan-10 11:09 AM

Create a DataSet out of it. Like:
DataSet myDataSet = new DataSet();

and then do a databind.
myControl.DataSource = myDataSet.Tables[tablename];

If that is not the case, then you could look at controls like XMLDataSource which allow you to do this:
Create a XMLDataSource in your markup like:
<asp:XmlDataSource ID="myXMLDataSource" runat="server" DataFile="~/mydatafilexml"></asp:XmlDataSource>

And bind it to your datacontrol as usual.

The below sample shows a dropdownlist bound to the XMLDataSource.

<asp:DropDownList ID="myDropDownLust" runat="server" DataSourceID="myXMLDataSource"
DataTextField="one" DataValueField="two">

And http://support.microsoft.com/kb/315906 is the Microsoft Knowledge base article titled "HOW TO: Display an XML Document in ASP.NET by Using the Xml Web Server Control" that talks about exactly this.

Hope that information suffices.
mv ark replied to Rakesh Vikram on 29-Jan-10 03:45 AM
Try the generic XSL stylesheet that is there in this article -

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">

<xsl:template match="/*">
        <xsl:for-each select="*[position() = 1]/*">
              <xsl:value-of select="local-name()"/>

<xsl:template match="/*/*">

<xsl:template match="/*/*/*">
    <xsl:value-of select="."/>


Multiple templates are involved in the transformation job. In each of the first three templates  we use the <xsl:apply-templates> action to tell the XSLT processor to carry on processing its child nodes & the nodes are processed recursively.