18.3. Transforming XML to HTML

Problem

The content for your application is in XML format, and you need to transform it to HTML for display in a browser.

Solution

Use an ASP.NET XML control and set its DocumentSource property to the XML document you need to transform and the TransformSource property to the XSLT document that specifies the transformation to be performed.

In the .aspx file, place an asp:Xml control where you want the HTML from the transformation to be placed in the page.

In the code-behind class for the page, use the .NET language of your choice to:

  1. Set the DocumentSource property of the XML control to the relative path to the XML document to convert.

  2. Set the TransformSource property to the relative path to the XSLT document.

Example 18-5 through Example 18-7 show the .aspx file and VB and C# code-behind files for an application that demonstrates this solution. The XML used as the source is shown in Example 18-8, and the XSLT used to transform the XML is shown in Example 18-9. The output transformed to HTML is shown in Figure 18-1.

Transforming XML to HTML output

Figure 18-1. Transforming XML to HTML output

Discussion

XML is quickly becoming the predominant format for storing content. XML provides a platform-independent format that can easily be converted to many other formats, including HTML. By storing the content for your web application in XML, the same content can be transformed to the HTML needed for ...

Get ASP.NET Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.