This article talks about how we can render XML data in an ASP.NET application. We will see how this can be done using
There are many scenarios when we need to handle
XML data in our ASP.NET applications. When we need to handle
and display them then we have 2 ways to do this. First way involved handling the
XML data in code and then parsing the
extracting the data into DataTables and then using this
DataSource with any of the ASP.NET server control.
The above mentioned technique is not only inefficient but also error prone becauase the developer will have the extract the
XML by writing code and then populate the
DataTables.This way of handling
XML data just for
presentation sometimes become very problematic if the data that we are recieving is
of more than 2 dimensions i.e. multiple tables coming in form of
XML. This is still a perfectly doable way of handling this data
but all the code that we write is overkill just to display the data.
Another way, and the recommended way, of handling
XML data just for presentation purpose if using
What we need to do in this approach is to define an
XSLT as per our presentatin requirements and then simply apply that
and what we get is the
ready to present to the user.
In this article, we will see how we can use
XSLT to prcess the
XML and display the data in the required format.
Using the code
Let us create a small application that will display a simple
XML data on a web page. The
XML contains contact information
of some people and we need to display this data in a tabular grid like format to the user. Lets look at the sample
<?xml version="1.0" encoding="utf-8" ?>
<Name>Sample Name 1</Name>
<address>Sample Address 1</address>
<Name>Sample Name 2</Name>
<address>Sample Address 2</address>
Now let us decide the format in which we want to show the data to the user.
Now let us write the
XSL to process the given
XML document and display it in the desired format.
<?xml version="1.0" encoding="utf-8"?>
<table width="500px" border="1px" style="text-align:left;border-collapse:collapse">
Note: To undertand all the details of
XSLT it is advisable to study the w3schools tutorials.
Now we have the
XSL file ready. How can we display this on the web page. This can be displayed on
the web page using ASP.NET
XML server control. We can place this server control anywhere on the page.
After this from the code behind we need to
DocumentContent property to the XML contents that needs to be displayed. Then we can set the
property of this
control to specify the
that should be used to generate the
this is done the resulting page will show the
data in the format that has been specified in our
protected void Page_Load(object sender, EventArgs e)
string xmlString = File.ReadAllText(Server.MapPath(@".\SampleData.XML"));
xmlContacts.DocumentContent = xmlString;
xmlContacts.TransformSource = Server.MapPath(@".\Contacts.xsl");
Now when we run the page we can see that the
XML data is visible in the format that we have specified in our
Point of Interest
Coming from a background of System Software development and C++, I didn't get chance to use the XSL transformations earlier to display the XML data in ASP.NET applications. I used to do it the hard way i.e. taking the XML data and putting it into
the Diconnected Data classes and then using these as DataSource with any ASP.NET server control. Very recently I got a chance
to work with huge amount of data coming in XML format. Also the data had more than 2 dimensions. To cater to that problem,
I used this approach of displaying the data. And since I already had the DataTable version of the application ready too, I could
see the visible performance improvements using this approach(as the amount of server side processing has been reduced a lot).
Most of the experienced programmers already know this stuff. This article is meant for the beginners. I hope this has been
- 03 October 2012: First version