views:

36

answers:

2

I've got a bit of XML I want to display on my ASP.NET website as-is (for debugging purposes), and it would be nice if it was colored. This should be easy to achieve with the right kind of XmlWriter, but I don't have the time to make one myself. Is there an existing (free) component that can do this?

A: 

That's going to be more a function of your editor than of the XML file itself, so no, XmlWriter won't do that.

Dave Markle
You misunderstand. I want my **webpage** to display a colorful bit of XML markup. I don't want to edit that XML or copy it to a file or anything. Just view it in a website.
Vilx-
Then just use a CSS file. CSS works great with XML. No need to emit any more XML from XMLWriter.
Dave Markle
How would that display the markup? Really, I want to see the **XML itself**. Like `<my_element some_attr="42">The meaning of life</my_element>` With attributes and their values and everything. And I want the syntax coloring for the XML. Like make angle brackets purple, and tag names blue, and text contents black, and attribute values yellow, and comments green, etc.
Vilx-
Then you'll need a browser that does syntax coloring.
Dave Markle
And display the XML in an iframe. It is a solution, but not the most elegant one. Would be nicer if I could get a proper HTML instead. I could even go along with having to define a bunch of CSS classes myself.
Vilx-
+2  A: 

I would not use an XmlWriter.

I'd use XSLT. If the XML file is to be displayed by itself, just embed a stylesheet PI into the XML: <?xml-stylesheet type="text/xsl" href="RawXmlAsHtml.xslt"?>. But since IE does this automatically, I'll assume your goal is not to display XML in its own page.

More likely the "raw" xml is to be displayed as part of another HTML page. In that case I'd use an XSL Transform on the server side to produce HTML from XML, then insert the output into a <asp:xml> control. Like this:

var doc= new System.Xml.XmlDocument();
doc.Load(xmlFile);
var xsl= new System.Xml.Xsl.XslTransform();
xsl.Load(Server.MapPath("RawXmlAsHtml.xslt"));
xml1.Document = doc;
xml1.Transform = xsl;

And the markup is:

<asp:xml id="xml1" runat="server" />

That leaves the question of, what XSLT can you use?

IE, since MSXML3, has included a stylesheet to format "raw" xml. It is sometimes accessible via res://msxml3.dll/defaultss.xsl . But this is not a XSLT standard stylesheet; it uses the Microsoft-specific WD-xsl format. It may not be what you want.

I looked and found something that complies to the XSLT standard; produced by Oleg Tkachenko and shipped as part of his eXml web control. It's available under a BSD-style license. (You might even want the entire exml control - I don't know what it is.)

Using that XSLT and the code above, the display looks like this:

alt text

It's not quite perfect, because that stylesheet generates a full HTML page, with <HTML> and <HEAD> tags, etc. You really just want a fragment. But you should be able to tweak it pretty easily, and anyway, it displayed properly for me, unmodified.


Edit: regarding the issue I mentioned: I modified the stylesheet to just not inject the <HTML> and <HEAD> tags. It works great.

Cheeso
Now this is a step in the right direction! Is my question really so badly formulated that this idea is so difficult to understand?
Vilx-
Not so difficult, but the XmlWriter part was a red herring, and may have thrown people off.
Cheeso
This just worked great for me. This should definitely be an accepted answer.
Brian Ramsay