We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 89,047 times

Contents

Related Categories

Reading, Storing and Transforming XML Data in .NET - The Transformation

DMarko1

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!

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 desktop applications. Till now Jimmy has authored nearly two dozen .NET articles, published on Dot Net Junkies, 4 Guys From Rolla, Sitepoint, MSDN Academic Alliance, Developers.NET, The Official Microsoft ASP.NET Site, and here on Developer Fusion, covering various unique and advanced techniques on .NET.

Comments

  • Posted by DMarko1 on 23 Mar 2005


    Hey Kurt,

    The xsl file is the xml transformation file which parses the xml into more readable html.

  • xsl

    Posted by kurtos on 07 Dec 2004

    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

  • Posted by James Crowley on 18 Jan 2004

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

  • Coding comment

    Posted by resalem on 12 Nov 2003

    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...