Displaying XML in ASP.NET

Web Server Controls Make It Easy

Don Kiely

October 30, 2009

3 Min Read
ITPro Today logo

TroubleshootingTips

LANGUAGES: All .NET Languages

ASP.NET VERSIONS: All

 

DisplayingXML in ASP.NET

WebServer Controls Make It Easy

 

By Don Kiely

 

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

 

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

 

  DocumentSource="XML/PizzaOrders.xml"   TransformSource="XML/Orders withTotal.xsl">

 

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

 

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

  • Document. If you have an in-memory instanceof the System.Xml.XmlDocument class containing the data, you can assignit directly to this property so that the source XML doesn't have to be parsedagain.

  • DocumentContent. If you have the raw XML as astring variable in memory, use this property so that the stream is read andparsed into the control. The result is the text content of the XML.

  • DocumentSource. Read the source XML as text froma disk file, as used in the above example.

  • Transform. Like the Documentproperty, use this property when the XSLT template is already parsed and inmemory.

  • TransformSource. Read the source XSLT as text froma disk file, as used in the above example.

 

Thisway, 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 theresulting transformation using the XML control. Here's a simple example ofusing the control this way:

 

Private SubPage_Load(ByVal sender As System.Object, _

  ByVal e As System.EventArgs) HandlesMyBase.Load

 

  Dim xmlDoc As XmlDocument = NewXmlDocument()

  xmlDoc.Load(Server.MapPath("xmlPizzaOrders.xml"))

 

  Dim xslTrans As XslTransform = NewXslTransform()

  xslTrans.Load(Server.MapPath("xmlOrders with Total.xsl"))

 

  XmlOut.Document = xmlDoc

  XmlOut.Transform = xslTrans

End Sub

 

The codeinstantiates XmlDocument and XslTransform objects to load the XMLand XSLT, then assigns those objects to the appropriate properties of the XMLcontrol. The control takes care of firing off the transformation.

 

There isnothing magical - or very little magical - about the XML control. It's simply ahandy way to write text to the Web page. If you give it raw XML, it will insertthat in the HTML output stream, which may not appear as anything on the pagethe 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 todisplay XML in an ASP.NET page, and it's about as simple as it gets.

 

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

 

 

 

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like