RSS

Search Engine

Sunday, May 16, 2010

ASP.Net XML Control Example with Document and Transform Properties

In this tutorial we will learn how to transform an XML document using Document and Transform property of ASP.Net XML control programmatically. You can load the XML document into the Document object and XSL file into the Transform object that enable you to format the XML document before writing it to the output stream. In the previous tutorial we learnt how to parse the XML content into the HTML format using inline DocumentSource and TransformSource properties of XML control but here we will use the C# code to create the XmlDocument object and XslTransform object to load and transform the provided Xml file. Let's take a look at short descriptions of Document and Transform properties to learn their implementation:

1. Document: It accepts the System.Xml.XmlDocument class object loaded with valid XML document that is to be displayed on web page using ASP.Net XML Control.

2. Transform: It accepts the System.Xml.Xsl.XslTransform class object loaded with valid Xsl Transformation style to parse the specified XML document into the desired HTML format.

If you will not pass the transform object to the XML control's Transform property then it writes the unformatted XML document content on the web page.

Example for ASP.Net XML control

HTML Code:

C# Code:

protected void Page_Load(object sender, EventArgs e)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(Server.MapPath("books.xml"));

XslTransform xslTransform = new XslTransform();
xslTransform.Load(Server.MapPath("CatalogFilter.xslt"));

Xml1.Document = xmlDocument;
Xml1.Transform = xslTransform;
}

To use the XmlDocument class and XslTransform class in your code behind you need to import the following Namespaces:

using System.Xml;
using System.Xml.Xsl;

The System.Xml namespace provides the access to the XmlDocument class and System.Xml.Xsl namespace enables you to use the XslTransform class. In the above sample code we have used the Server.MapPath method to specify and load the Xml document and Xsl file to their respective XmlDocument object and XslTransform object. Further we have set the Document and Transform properties of ASP.Net XML control dynamically. It will transform the specified XML file into the HTML format provided in the XSL transformation style sheet loaded in the transform class object.

You can use the XML file and XSL file that we used in the previous tutorial: ASP.Net XML Control Example with DocumentSource and TransformSource Properties.

0 comments:

Post a Comment