LANGUAGES: All .NET Languages
ASP.NET VERSIONS: All
Displaying XML in .NET
So Many Ways, So Little Time
By Don Kiely
XML has many potential uses in any application, not all of which involve presenting information to a human user. Some uses, such as making a Web services call using SOAP, may involve XML only tangentially; the result may be a single number returned from a server. One of the most common uses of XML today is to present information to a user.
But what options do you have for displaying XML? There are many ways to display XML in .NET applications - so many that the problem is deciding which is best for your particular application.
Raw Data in the Browser
It will be rare that you'll want to display raw XML to a user as part of a production application, but it can be a useful technique during development to examine XML data generated on the fly when you run your application.
This works well with Internet Explorer and Mozilla FireFox because they have their own built-in XSLT templates used when the content type of the data is one of the xml variations, most commonly text/xml. There are many ways to do this programmatically, as well; I'll examine those in future columns.
The figure below shows this default rendering of XML data in FireFox, my browser of choice these days. You can expand and contract different levels of the XML hierarchy, and as the note indicates, it will automatically apply any formatting sheets it finds referenced in the XML.
innerHTML Property of a Placeholder Control
Dynamic HTML (DHTML) is a useful way to add interactivity to a Web page, without requiring an HTTP call to the Web server every time the user clicks an object in the page. Although originally designed to support client-side scripting code, the features it adds to objects on the page are available to your server-side code in an ASP.NET application.
One of the most useful for displaying XML on a Web page is the innerHTML property of most controls. By setting this property to a string that contains XML or HTML, you can dynamically generate and display XML at run time. (There is a corresponding outerHTML property, but it's less useful because it overwrites the container control from the Web page. This means that the
Next time I'll look at using built-in Web server controls for displaying and formatting XML data.
Don Kiely is senior technology consultant for Information Insights, a business and technology consultancy in Fairbanks, AK. E-mail him at mailto:[email protected].