Instructor-led online courses in ASP.NET Core, ASP.NET MVC, and ASP.NET Design Patterns. Read more...
Instructor-led online courses in ASP.NET Core, ASP.NET MVC, and ASP.NET Design Patterns. Conducted by Bipin Joshi on weekends. More details are available here.

<%@ Page %>

Using XML Web Control

Introduction

Now a days use of XML is increasing quickly. Many times data that you want to display can be stored in XML files rather than statically putting it in HTML pages. Such XML data can be transformed to HTML by applying XSL style sheet to it. In traditional ASP, we used XSL Transform objects to achieve that. ASP.NET makes displaying XML documents in browser much easy via a special web control called XML control.

Creating XML Document

For our example we will create following XML document. Just type in some data matching following following structure in any text editor and save the file as mybooks.xml
<?xml version="1.0" encoding="utf-8" ?>
<books>
<book>
<title>Title 1</title>
<publisher>Publisher 1</publisher>
</book>
</books>

Creating XSL Style Sheet

Now, let us create an XSL style sheet that transforms above data as HTML table.
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<table border="1">
<xsl:for-each select="books/book">
<tr>
<td><xsl:value-of select="title">
</xsl:value-of></td>
<td><xsl:value-of select="publisher">
</xsl:value-of></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Here we simply transform source XML to HTML.

Applying XSL to XML and outputting the results

Finally we will use ASP.NET XML web control that does this task. Following properties of the XML control are important:
  • DocumentSource : That specifies the XML document path
  • TransformSource : That specifies XSLT style sheet path
In order to use this control simply add following markup in your web form.
<asp:Xml id="Xml1" runat="server"
DocumentSource="mybooks.xml"
TransformSource="myxsl.xslt">
</asp:Xml>
Now, run the web form and you should get books data displayed in HTML table. Note that XML web control also allows you to use XML strings or even XML dom documents as source.

Summary

In this article we saw how to quickly use ASP.NET XML web control to display XML documents in ASP.NET pages.

Bipin Joshi is a software consultant, trainer, author and spiritual yoga mentor having 23+ years of experience in software development. He teaches online training courses in ASP.NET Core, Angular, and Design Patterns to individuals and small groups. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 24 May 2002


Tags : ASP.NET Server Controls XML


Subscribe to our newsletter

Get monthly email updates about new articles, tutorials, code samples, and how-tos getting added to our knowledge base.