View Xml in webbrowser control

I have to embend webbrowser control into my application, so that I can show pieces of Xml code more nicely.I found two ways of doing this.The first and easiest was just to save the xml code in temporary xml file and navigate the webbrowser to it :

// save xml file and load it in XmlWebBrowser
XmlTextWriter wr = new XmlTextWriter("temp.xml",Encoding.UTF8);
node.WriteTo(wr);
wr.Close();
this.XmlwebBrowser.Navigate(Application.StartupPath + "\\temp.xml");

The second way was to show the code without saving it in temporary file.Doing it this way the xml code appears in webbrowser without being formated ( raw code).So before showing it I have to combine it with stylesheet similar to the default Internet Explorer stylesheet and after converting it into html format using XslCompiledTransform I was able to show it:

/// Trasform Xml to Html
XslCompiledTransform objTransform = new XslCompiledTransform();
StringWriter objStream = new StringWriter();
// load xml default style sheet
objTransform.Load("xmlprint.xsl");
objTransform.Transform(node,null,objStream);
string html = objStream.ToString();
// load html page
this.XmlwebBrowser.DocumentText = html;

Some usefull links:

Xslt Stylesheet highlighter

Xslt in MSXML

Default Xml Stylesheet for IE

3 responses to this post.

  1. Posted by naveed on 2008/07/02 at 11:17 am

    thanks a lot, buddy.

    you saved my lot of time.

    Reply

  2. Posted by Joe Mosley on 2009/08/13 at 11:46 am

    Thanks a lot, worked a dream and just what i needed.

    Reply

  3. Posted by Anton Perelyubskiy on 2010/08/05 at 8:41 am

    Thank you!

    Exactly what I needed.

    Reply

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: