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

2 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

Respond to this post