Library tutorials & articles

Reading, Storing and Transforming XML Data in .NET

The Transformation

The Transformation

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HTML><BODY topmargin="0"><TABLE BORDER="1" WIDTH="100%">
<BR/>
<TR STYLE="font-size:10pt; color:blue; text-decoration:none;font-weight:bold;" align="center">
<TD STYLE="background-color:white">Id:</TD>
<TD STYLE="background-color:white">Author:</TD>
<TD STYLE="background-color:white">Phone:</TD>
<TD STYLE="background-color:white">Address:</TD>
<TD STYLE="background-color:white">Contract:</TD>
</TR>
<xsl:for-each select="PubsList/Pubs">
<TR STYLE="font-size:10pt; color:black; padding:0px 6px" align="center">
<TD width='20%'><xsl:value-of select="au_id"/></TD>
<TD width='25%'>
<xsl:value-of select="au_fname"/> <xsl:value-of select="au_lname"/>
</TD>
<TD width='20%'><xsl:value-of select="phone"/></TD>
<TD width='40%'>
<xsl:value-of select="address"/> 
<xsl:value-of select="city"/>, <xsl:value-of select="state"/>  <xsl:value-of select="zip"/>
</TD>
<TD width='20%'><xsl:value-of select="contract"/></TD>
</TR>
</xsl:for-each></TABLE>
<BR/>
</BODY></HTML>
</xsl:template>
</xsl:stylesheet>

Ok and This All Means What?

Well, it's like HTML but...not quite. Actually, all the above is what will transform our XML into readable HTML with a format that's easy on the eyes.

What we do here first in line one is write out our XML declaration (or processing instructions). Then move to the <xsl:template match="/"> which defines or matches our whole document, which we then intersperse with HTML markup.

After we markup up our header row, we now begin looping through each of our child nodes that begin with Pubs (which denote each row of data contained within our parent or root node PubsList). If you open and display the raw XML in your browser you'll notice that PubsList is the parent node enclosing all the Pubs child nodes and their elements. The transformation below is simply setting up a loop which iterates through them,

<xsl:for-each select="PubsList/Pubs">

whereby the formatting occurs with each element being wrapped in some HTML tags as in one datarow example below:

<TD width='20%'><xsl:value-of select="phone"/></TD>

At the end of the XSL template, you'll observe the loop's closing tags and the final HTML tags. In case your wondering what the &#160; is? Well it's simply XSL's way of writing a blank space.

There... not too difficult, was it? If you are still finding it a little difficult to grasp the fine art of XSL transformations, you'll find plenty of resources on the web to help you since this is beyond the scope of this article. Even so, within the context of this article, it essentially shouldn't be to difficult to follow.

The Datagrid

Now for something even less demanding. Providing you've used the methods above to produce an XML document on your local disk, or anyone for that matter, binding to a DataGrid Web Control will require only four lines of code! Have a look.

void Get_XML_DataGrid(){

    DataSet myDataSet = new DataSet();

    myDataSet.ReadXml(Server.MapPath("pubs.xml"));

    xmlDG.DataSource = myDataSet;
    xmlDG.DataBind();

}

And instead of writing the results out by using the XML Web control, you now can use the DataGrid control like so:

<asp:DataGrid id="xmlDG" runat="server" />

So now simply replace the Get_XML_Data() method with the Get_XML_DataGrid() method and swap the two output controls and you're set. Once more, make sure there is XML to read. Moreover, the Datagird web control presented above is the absolute most simplest configuration. Be sure to read the .NET tutorials on dressing it up to your liking and even on adding paging to it as well.

Be sure to check out my other article which goes into greater detail on paging a Datagrid with exact count. I'll leave it up to you .NET wizards to merge the two articles in one cool app!

Comments

  1. 23 Mar 2005 at 14:16

    Hey Kurt,

    The xsl file is the xml transformation file which parses the xml into more readable html.
  2. 07 Dec 2004 at 07:40

    i might be very slow here, but what part of the code here are supposed to be used in the pubs.xsl file? yes, I am a newbie :-)


    Regards,
    Kurt
    Norway

  3. 18 Jan 2004 at 18:27

    Thanks - I'll check that and edit the article to fix it

  4. 12 Nov 2003 at 13:31

    Very good example, simple but contains a lot of information. Only one part I had to modify, in the XSL I had to change the criteria for the select from "PubsList/Pubs" to "PubsList/Table" as the child node is table and not pubs. I also had to grant write access to ASPNET account for the folder where I'm creating the XML.
    Thanks,


    Reem

  5. 01 Jan 1999 at 00:00

    This thread is for discussions of Reading, Storing and Transforming XML Data in .NET.

Leave a comment

Sign in or Join us (it's free).

Dimitrios Markatos Dimitrios, or Jimmy as his friends call him, is a .NET developer/architect who specializes in Microsoft Technologies for creating high-performance and scalable data-driven enterprise Web and deskto...

Related discussion

Related podcasts

Events coming up

  • Mar 15

    DevWeek 2010

    London, United Kingdom

    DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.

Want to stay in touch with what's going on? Follow us on twitter!