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

Hi,

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.

Regards,
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();
myDataSet.ReadXml(@"x:\xmlfiles\myxml.xml");

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

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">
</asp:DropDownList>

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 -
http://eggheadcafe.com/tutorials/aspnet/58b72f32-8da6-44e3-8c9a-61aebdf6be6b/xsl-transformation-to-htm.aspx

Excerpt:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
<xsl:template match="/">
<HTML>
<BODY>
    <xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>

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

<xsl:template match="/*/*">
<TR>
    <xsl:apply-templates/>
</TR>
</xsl:template>

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

</xsl:stylesheet>


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.