views:

1510

answers:

3

I have some XML in an XmlDocument, and I want to display it on an ASP.NET page. (The XML should be in a control; the page will have other content.) Right now, we're using the Xml control for that. Trouble is, the XML displays with no indentation. Ugly.

It appears that I'm supposed to create an XSLT for it, but that seems kind of boring. I'd rather just throw it into a control and have it automagically parse the XML and indent correctly. Is there an easy way to do that?

+2  A: 

A quick (and dirty) way of doing this would be to use an IFrame.

In truth, an XSLT is the "ideal" way for formatting an XML for display. Another option would be to parse it manually for display.

To use an Iframe: ASPX side:

< iframe runat="server" id="myXMLFrame" src="~/MyXmlFile.xml" /></pre>

Code Side:

myXMLFrame.src = Page.ResolveClientUrl("~/MyXmlFile.xml")
Stephen Wrighton
i'd use an iframe as well
Shawn Simon
Can you be more specific? It's not clear to me what I'd need to do in code to populate the iframe.
Kyralessa
@Kyralessa - see my edits
Stephen Wrighton
How can I do it if the XML is in a string instead of a file? (Presumably there's some way without having to write out a temp file.)
Kyralessa
1 way is to have the IFrame redirect to your same page, passing it a query string variable to tell it to output the XML (via a response.write), and then change the page's content type to XML, finally call a response.end so you don't continue the page's lifecycle
Stephen Wrighton
+3  A: 

You could try to use XmlWriter/XmlTextWriter, set the writer's Indentation property, write to a StringBuilder or MemoryStream, and output the result inside a <pre> tag

devio
<pre> tag, that's what I was missing. Thanks. After I posted, I found some sample code for creating the formatted XML here: http://forums.asp.net/t/1145533.aspx
Kyralessa
A: 

You can find a slightly modified version of the XSLT that IE uses to transform XML to HTML when viewing in IE at http://www.dpawson.co.uk/xsl/sect4/N10301.html#d15977e117.

I have used it in a WebBrowser control in a WinForms application, and it works lika a charm. I have not tested it in FireFox/Chrome/Safari/Operat, though.