h1

Displaying XML in the .NET WebBrowser Control

October 17, 2006

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
{
set
{
Stream s = <defaultss.xsl from embedded resource file>

XmlReader
xr = XmlReader.Create(s);
XslCompiledTransform xct = new XslCompiledTransform();

xct.Load(xr);

StringBuilder
sb = new StringBuilder();
XmlWriter xw = XmlWriter.Create(sb);
xct.Transform(value, xw);

this.DocumentText = sb.ToString();
}
}

Turned out kind of neat in the end, huh?

Advertisements

25 comments

  1. Thx a lot!


  2. Excellent! Still seems retarded that M$ didn’t embed the proper defaultss.xsl into the control to start with, and expose and XmlDocument property, and perhaps and XmlString property as well… Using your magic, and the file converted by Steve Muench I was easily able to extend the webbrowser control to do both… Thanks for pointing the way!


  3. HI,

    Thnax for so nice article.

    I have implemented it in my code..but it is facing one problem when i put MessageBox.show(sb.tostring()) at that time it shows data else it dosen’t work..

    I need it fast..

    pls mail me at : surajkikiran1@gmail.com

    Thanx
    Kiran Patil


  4. an absolutely great idea.
    It works and it works perfectly!

    Thank you for the article and for providing Mr. Muench’s file.

    vt


  5. I am showing a pdf in webBrowser control on a winform application. I want to save it on the hard disk i use webBrowser.ShowSaveASDialog() but it works in windows server 2003 but in XP Sp2 it doesnot show the Acrobat save dialog window rather it shows iE save dialog.

    Any body with any useful comments to get hold of Acrobat save dialog.

    Thanks in advance


  6. That looks like a really useful piece of code. Unfortunately I’m lacking the additional knowledge required to use it – i.e. how to extend the browser base class & how to embed & use the xsl as a resource. If you could post a complete sample program that would be very useful!


  7. This works slick, however, I’m noticing that if your IE (engine) has its security set lower this code will generate an untrusted site dialog “about::security_yourappname”. Anyone else see this or know what’s causing it? Thanks.


  8. very interesting, but I don’t agree with you
    Idetrorce


  9. Thanks. What’s wierd in my case is that I have an XML string that will display/render fine if I save it and then open it in IE, but using the code above and the associated XSL, I get an exception thrown with “invalid chars”. The XSL renders fine in IE too. I’m puzzled.


    • Try
      XmlWriter xw = XmlWriter.Create(sb, new XmlWriterSettings { CheckCharacters = false });
      rather than
      XmlWriter xw = XmlWriter.Create(sb);


  10. This solution is indeed pretty neat. And thanks for providing the defaultss.xsl stylesheet.


  11. Thank you for sharing this information. It helped me a great deal. I have seen others solve the problem by writing their XML to a temp file and then loading it into the WebControl. Your approach is much better.


  12. […] some one has modified the build in IE XSL to bring that same ability to the .Net browser control.  Read more here. RSS Filed under: General Well, I am still busy with life… […]


  13. awesome, thanks … I created a new component and inherited the web browser in the partial class. Added the new property, compiled and dropped the new component onto the form from the toolbox … works like a charm.


  14. This works great !!! my PM was happy to see this 🙂


  15. Excellent. Thanks.


  16. Thanks again for the article! Works like a charm!


  17. Hey,
    That link to the xsl doesn’t work. I get a WordPress need to authenticate error.

    Thanks!


  18. please publish another time the xsl file and publish too the final project because you leave many ideas to work please give us the entire source code


  19. The link doesn’t work 😦


  20. why don’t you make it available for the public without any additional overhead..that helps a lot


  21. What do I need to do to get the files?


  22. all I get is an empty html tag after transformation stripping out all my xml data



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: