Rendering XML Data as HTML using XSL Transformation and ASP.NET XML Control

This article talks about how we can render XML data in an ASP.NET application. We will see how this can be done using XSLT transformations. 

Background

There are many scenarios when we need to handle XMLdata in our ASP.NET applications. When we need to handle XMLdata and display them then we have 2 ways to do this. First way involved handling the XMLdata in code and then parsing the XMLand extracting the data into DataTables and then using this DataTablesas DataSourcewith 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 data from XMLby writing code and then populate the DataTables.This way of handling XMLdata 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 XMLdata just for presentation purpose if using XSLTtransformations. What we need to do in this approach is to define an XSLTas per our presentatin requirements and then simply apply that XSLTon our XML and what we get is the HTMLready to present to the user.

In this article, we will see how we can use XSLTto prcess the XMLand display the data in the required format.

Using the code

Let us create a small application that will display a simple XMLdata on a web page. The XMLcontains 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 XMLfirst.

Now let us decide the format in which we want to show the data to the user.

format

Now let us write the XSLto process the given XMLdocument and display it in the desired format.

Note: To undertand all the details of XSLTit is advisable to study the w3schools tutorials.

Now we have the XMLand XSLfile ready. How can we display this on the web page. This can be displayed on the web page using ASP.NET XMLserver control. We can place this server control anywhere on the page.

DesignView

After this from the code behind we need to set the DocumentContentproperty to the XML contents that needs to be displayed. Then we can set the TransformSourceproperty of this XMLcontrol to specify the XSLthat should be used to generate the XHTMLfrom this XML. Once this is done the resulting page will show the XMLdata in the format that has been specified in our XSLfile.

Now when we run the page we can see that the XMLdata is visible in the format that we have specified in our XSLfile.

Screenshot

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

Download sample code for this article: XSLTDemo