• >

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

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Slashdot
  • Technorati
  • Ping.fm
  • Reddit

Related posts:

  1. EXSLT
  2. Assosiating file type to your application
  3. Insert/Read image from SQL Database
  4. Setting FileSystemWatcher

This entry was posted in C#, Programming, XML and tagged , , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.

One Comment

  1. naveedNo Gravatar
    Posted July 2, 2008 at 11:17 am | Permalink

    thanks a lot, buddy.

    you saved my lot of time.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • isisun photos

  • Categories

  • posidev.com

    Valid XHTML 1.0 Transitional

    Tracked by ClickAider

  • Charts

  • Visitors

    free counters