Integrate XML Data Within your ASP Page Using XslCompiledTransform Class: Part I

Many people encountered problems and they don't know how to transform data and integrate them within a given asp web page contents. This particular question is posed frequently in many forums. Therefore I will provide two techniques to achieve this goal. A classic way which will be presented as a part of this part I tutorial, this classic way makes use of the XSL transformation to transform an XML file, and then the given formatted content will be integrated later within a given ASP page. The second technique consists on using XLINQ and it will be exposed in the second part of this tutorial.

First, let's suppose this simple xml file

<?xml version='1.0'?>

<Countries>

  <Country>

    <Name>Lybia</Name>

    <Area>1,759,541 km2</Area>

    <Population>6,173,579</Population>

    <Capital>Triapolis</Capital>

    <Currency>Dinar</Currency>

    <TLD>ly</TLD>

 

  </Country>

 

  <Country>

    <Name>Tunisia</Name>

    <Area>     163,610 km2</Area>

    <Population>10,327,800</Population>

    <Capital>Tunis</Capital>

    <Currency>Dinar</Currency>

    <TLD>tn</TLD>

 

  </Country>

 

  <Country>

    <Name>Algeria</Name>

    <Area>     2,381,741 km2</Area>

    <Population>33,769,669</Population>

    <Capital>Alger</Capital>

    <Currency>Dinar</Currency>

    <TLD>dz</TLD>

 

  </Country>

 

  <Country >

    <Name>Morroco</Name>

    <Area>     446,550 km2</Area>

    <Population>31,352,000</Population>

    <Capital>Ribat</Capital>

    <Currency>Dirham</Currency>

    <TLD>ma</TLD>

 

  </Country>

 

  <Country>

    <Name>Mauritania</Name>

    <Area>1,030,700 km2</Area>

    <Population>3,069,000</Population>

    <Capital>Nouag Chot</Capital>

    <Currency>Ouguiya</Currency>

    <TLD>mr</TLD>

 

  </Country>

</Countries>

The purpose here is to represent its contents within ASP page. First, let's create an XSL file. The manner of how XSL file is structured and built is out of the scope of this tutorial but nevertheless and in order to understand how to build an XSLT file from A to Z I propose this good tutorial

http://www.w3schools.com/xsl/

Now this the content of the XSL file:

<?xml version='1.0'?>

<xsl:stylesheet version="1.0"

                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

                xmlns:msxsl="urn:schemas-microsoft-com:xslt"

                exclude-result-prefixes="msxsl">

 

  <xsl:output method="html" indent="yes"/>

  <xsl:template match="/">

    <h1>North African countries</h1>

    <h2>Area of each state</h2>

    <table>

    <xsl:for-each select="Countries/Country">

      <tr>

        <th align="left"><xsl:value-of select="Name"/></th>

        <td><xsl:value-of select="Area"/></td>

      </tr>

    </xsl:for-each>

    </table>

 

    <h2>Population in each state</h2>

    <table>

      <xsl:for-each select="Countries/Country">

        <tr>

          <th align="left">

            <xsl:value-of select="Name"/>

          </th>

          <td>

            <xsl:value-of select="Population"/> habitants

          </td>

        </tr>

      </xsl:for-each>

    </table>

 

    <h2>The capital of each state</h2>

    <table>

      <xsl:for-each select="Countries/Country">

        <tr>

          <th align="left">

            <xsl:value-of select="Name"/>

          </th>

          <td>

            <xsl:value-of select="Capital"/>

          </td>

        </tr>

      </xsl:for-each>

    </table>

 

    <h2>The currency of each state</h2>

    <table>

      <xsl:for-each select="Countries/Country">

        <tr>

          <th align="left">

            <xsl:value-of select="Name"/>

          </th>

          <td>

            <xsl:value-of select="Currency"/>

          </td>

        </tr>

      </xsl:for-each>

    </table>

 

    <h2>The TLD of each state </h2>

    <table>

      <xsl:for-each select="Countries/Country">

        <tr>

          <th align="left">

            <xsl:value-of select="Name"/>

          </th>

          <td>

            <xsl:value-of select="TLD"/>

          </td>

        </tr>

      </xsl:for-each>

    </table>

  </xsl:template>

</xsl:stylesheet>

Now, the both files, I mean the XML file and XSL file, should be saved somewhere in the hard drive, let's say D:\NorthAfrica.xml and D:\Transformation.xslt. Indeed, visual Studio gives us the possibility to Test and debug our XSLT file. I will show how to leverage that.

First, open the XML file within Visual studio

1.gif
 
Figure 1
 
As you can observe thexml file is opened within Visual Studio

2.gif
 
Figure 2


Afterward and in order to debug the XSL file, you press the icon 

3.gif
 
Figure 3

Of Corse, if the XML editor tool bar is not visible then you right click somewhere on the tool bar zone and then check the XML editor menu Item

4.gif
 
Figure 4

After pressing the icon icon.gif. If it is the first time, the debugger invites you to precise which XSL file will be applied to our XML file, then you browse and define the related XSL file.

5.gif
 
Figure 5

After clicking the Open Dialog box button the result will be:

6.gif

Figure 6

After debugging our XSL file, we step forward to the next stage which is generating this resulting content at the Default.aspx page level. To do that, I will use an XslCompiledTransform over an XslTransform because this last one becomes obsolete and the XslCompiledTransform integrates some new features over the XslTransform.

To understand the difference between the both classes, I propose this MSDN tutorial
http://msdn.microsoft.com/en-us/library/66f54faw.aspx

This is the code that I developed to integrate the XML transformed content within the Default.aspx page.

using System;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Xml;

using System.Xml.XPath;

using System.Xml.Xsl;

using System.IO;

using System.Text;

namespace myWebApplication

{

    public partial class _Default : System.Web.UI.Page

    {

     protected void Page_Load(object sender, EventArgs e)

     {

      try

       {

               

        /*Create the XslCompiled object, I put true as argument within the

         constructor to enable the xslt object debug the xslt file before execute

         the transformation but once your tests are done you can put it to

         fale or simply define a parameterless constructor*/

         XslCompiledTransform xslt = new XslCompiledTransform(true);

 

         //Load the XSLT file

          xslt.Load(@"D:\Transformation.xslt");

 

         //Load the xml file into a file stream

          FileStream xmlStream = new FileStream(@"D:\NorthAfrica.xml", FileMode.Open);

 

         //Create an XmlReader object

          XmlReader oReader = new XmlTextReader(xmlStream);

 

        //This string builder is necessary for the TextWriter next

         StringBuilder Content = new StringBuilder();

      

        //The string builder serves as argument to overload the txtWriter Constructor

         TextWriter txtWriter = new StringWriter(Content);

       

        //Create an XmlWriter object

         XmlWriter oWriter = new XmlTextWriter(txtWriter);

               

        // Execute the transform process and output the results to the asp page.

         xslt.Transform(oReader, oWriter);

         Response.Write(txtWriter.ToString());

       }

       catch (FileNotFoundException caught)

       {

          Response.Write(caught.Message);

       }

       catch (XmlException caught)

       {

           Response.Write(caught.Message);

        }

        catch (IOException caught)

        {

          Response.Write(caught.Message);

        }

 

           

        }

    }

}

The output will be as follow:

7.gif

Figure 7

That's it. In the next tutorial, I will propose the second technique in order to attempt the same goal but using XLINQ.

Good Dotneting!!!

Up Next
    Ebook Download
    View all
    Learn
    View all