X
Business

Create ASP.NET pages from XML data

The server control in .NET allows you to insert an XML document into an ASP.NET page. Find out what classes you need to use and how you can try out this approach.
Written by Leonardo Esposito, Contributor

A lot of ASP applications produce their output based on the contents of one or more XML files. XSLT documents transform the browser-neutral contents of those XML files into browser-specific HTML. The transformation is always accomplished through VBScript or JScript code, and the results are appended to the output stream.
In ASP.NET, the implementation of the same technique is made significantly easier and more flexible by the server control. This control functions as a placeholder to inject the contents of an XML document directly into an ASP.NET page. The control can display the source of an XML document verbatim or display the results of an XSLT transformation.
The control
In .NET, the XslTransform class performs the XSLT transformation. The class takes the source document and the style sheet and returns either XML or HTML output. The following code snippet shows the few lines of code you need to use the class:

    XslTransform xslt = new XslTransform();
    xslt.Load(stylesheet);
    xslt.Transform(source, output);

The control operates as a sort of declarative counterpart for the XslTransform class and makes use of the class internally. You use the control when you need to embed XML documents in a Web page. For example, the control comes in extremely handy when you need to create XML data islands that the client will utilise. The snippet below shows how to embed the output of the server control into a client tag that Internet Explorer will interpret as an XML data island. In this case, the XML source is written as inline code:

    <xml id="theXml">
    <asp:xml runat="server">
    ... xml data ...
    </asp.xml>
    </xml>

You can specify a source document in various ways and not just as inline code. You can use an external file, a string, and even an instance of the .NET XML document object model.

A style sheet can be specified through a file or using a preconfigured XslTransform object. The output of the transformation, if any, is the Web page output stream. Table A shows the properties of the control.

Table A: Control Properties
Property Description
Document  Gets or sets the XML source document using an XmlDocument
DocumentContent Gets or sets the XML source document using a string
DocumentSource Gets or sets the XML source document using a file
Transform Gets or sets the XslTransform class to use for transformations
TransformSource Gets or sets the stylesheet to use for transformations
TransformArgumentList Gets or sets the argument list for transformations

All the settings for the document and the transformation are mutually exclusive, and the last setting wins. For example, if you set both Document and DocumentSource, no exception is thrown, but the first assignment is overridden.

Using the control
Listing A demonstrates how to set up an XML-to-HTML transformation using a style sheet document.

Listing A

<asp:xml runat="server" TransformSource="EmpInfo.xsl">
<MyDataSet>
<NorthwindEmployees>
<Employee>
<employeeid>1</employeeid>
<firstname>Nancy</firstname>
<lastname>Davolio</lastname>
<title>Sales Representative</title>
<notes>...</notes>
</Employee>
</NorthwindEmployees>
</MyDataSet>
</asp:xml>

The control can have an ID and can be programmatically accessed. For instance, you can check the browser's capabilities and decide dynamically what style sheet would work best.

You could also describe the whole page with XML and employ a style sheet to translate the document to HTML. However, this is not always the best solution to gain flexibility and results in a poor extensibility model. But the control definitely makes implementing that solution considerably easier:

    <asp:xml runat="server" id="theXml"
    DocumentSource="Employees.xml"
    TransformSource="EmpInfo.xsl" />

If you need to pass in an argument, just create and populate an instance of the XsltArgumentList class and pass it to the control using the TransformArgumentList property:

    XsltArgumentList args = new XsltArgumentList();
    DateTime today = DateTime.Now;
    args.AddParam("orderdate", "", d.ToString());
    theXml.TransformArgumentList = args;

The ASP.NET page in Listing B contains an XML data island created using the control and includes some VBScript code that retrieves and displays the content of the data island.

The following example demonstrates multibrowser programming. The XML control is bound to a particular file but no transformation style sheet is statically declared:

    <asp:xml runat="server" id="theXml"
    DocumentSource="Employees.xml" />

At runtime, the host page determines the calling browser and applies the transformation that best fits:

    theXml.TransformSource = "standard.xsl";
    if (Request.Browser.Type ToUpper() == "IE5")
    theXml.TransformSource = "ie.xsl";

The Request.Browser property detects the type and the capabilities of the underlying browser.

Conclusion
In ASP.NET, you can use the server control to accomplish a common task of ASP applications--converting some generic XML document to browser-specific code. The control works as a placeholder for the final XML or HTML text generated by the XML source file and the optional style sheet. The programming interface of the control closely reflects that of the .NET class devoted to XSL transformations, XslTransform.

Editorial standards