Displaying XML in ASP.NET

Web Server Controls Make It Easy

Troubleshooting Tips

LANGUAGES: All .NET Languages

ASP.NET VERSIONS: All

 

Displaying XML in ASP.NET

Web Server Controls Make It Easy

 

By Don Kiely

 

One of the slickest innovations in ASP.NET is Web server controls - drop-in components that dynamically generate HTML at run time based on the user's Web browser, properties you set, and server-side code. Microsoft ships many useful server controls with .NET, and third-party controls have flooded the market. When working with XML, some of these controls make it easy to display XML and other data. Several of the server controls accept XML directly for display, such as the DataList, DataGrid, and Repeater controls.

 

By far the easiest way to display XML data in ASP.NET is by using the XML Web server control. With its simple interface and limited features, it's ideally suited to straightforward XML display. The easiest way to use the control is to set the DocumentSource and TransformSource properties at design time to the XML files you want to transform:

 

<asp:Xml id="Xml1" runat="server"

  DocumentSource="XML/Pizza Orders.xml"

  TransformSource="XML/Orders with Total.xsl">

</asp:Xml>

 

When users request the page, they get the HTML generated as a result of applying the XSLT transformation. Using the control this way, you don't have to write a single line of code, and you can still use a Cascading Style Sheet (CSS) to format the resulting HTML. The XML control does all the processing on the server so the client browser simply sees the generated HTML.

 

Another, more flexible, way to use the XML control is to leave the source document settings blank at design time, then set them programmatically when the user requests the page. The control has five run-time properties:

  • Document. If you have an in-memory instance of the System.Xml.XmlDocument class containing the data, you can assign it directly to this property so that the source XML doesn't have to be parsed again.
  • DocumentContent. If you have the raw XML as a string variable in memory, use this property so that the stream is read and parsed into the control. The result is the text content of the XML.
  • DocumentSource. Read the source XML as text from a disk file, as used in the above example.
  • Transform. Like the Document property, use this property when the XSLT template is already parsed and in memory.
  • TransformSource. Read the source XSLT as text from a disk file, as used in the above example.

 

This way, you can do whatever run-time processing of the XML and XSLT data you need, such as reading it from a database and filtering it, then easily show the resulting transformation using the XML control. Here's a simple example of using the control this way:

 

Private Sub Page_Load(ByVal sender As System.Object, _

  ByVal e As System.EventArgs) Handles MyBase.Load

 

  Dim xmlDoc As XmlDocument = New XmlDocument()

  xmlDoc.Load(Server.MapPath("xml\Pizza Orders.xml"))

 

  Dim xslTrans As XslTransform = New XslTransform()

  xslTrans.Load(Server.MapPath("xml\Orders with Total.xsl"))

 

  XmlOut.Document = xmlDoc

  XmlOut.Transform = xslTrans

End Sub

 

The code instantiates XmlDocument and XslTransform objects to load the XML and XSLT, then assigns those objects to the appropriate properties of the XML control. The control takes care of firing off the transformation.

 

There is nothing magical - or very little magical - about the XML control. It's simply a handy way to write text to the Web page. If you give it raw XML, it will insert that in the HTML output stream, which may not appear as anything on the page the user views after the browser is done with it. If you want valid HTML, you'll have to transform it as we've shown above. But it's a great way to display XML in an ASP.NET page, and it's about as simple as it gets.

 

Don Kiely is senior technology consultant for Information Insights, a business and technology consultancy in Fairbanks, AK. E-mail him at mailto:[email protected].

 

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish