Internet Explorer displays XML in a pretty decent manner. Today I figured I would use the same engine to display XML in a .NET 2.0 application. So I dropped a WebBrowser control on to a form, and assigned my XML string to the DocumentText property. Voila? Of course not.
After some digging, I found that IE uses a built-in XSLT-transform to present XML as HTML in the browser. The stylesheet in question is available from the following uri in IE: res://msxml.dll/defaultss.xsl. Ok then, so one can just save this string, and use it in .NET? Not quite. As it turns out, the XSL that IE uses is not compatible with .NET’s XslCompiledTransform class. D’oh!
After some more digging, it turns out that Steve Muench has done the conversion to the XSLT 1.0 REC that .NET requires. Thanks, man! The file can be downloaded here.
I added the following code to my WebBrowser base class, to allow developers to display XML by setting a property:
public XmlDocument DocumentXml
Stream s = <defaultss.xsl from embedded resource file>
XmlReader xr = XmlReader.Create(s);
XslCompiledTransform xct = new XslCompiledTransform();
StringBuilder sb = new StringBuilder();
XmlWriter xw = XmlWriter.Create(sb);
this.DocumentText = sb.ToString();
Turned out kind of neat in the end, huh?