Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display XML with WebBrowser control?

Tags:

c#

xml

winforms

I have a string that contains a xml. I want to set the value of the WebBrowser control with it and display as a xml.

I can set the value with browser.DocumentText, but how do I tell it to display as a XML ?

like image 613
BrunoLM Avatar asked Mar 01 '11 16:03

BrunoLM


2 Answers

To give some code to the first solution @PaoloFalabella suggested (i.e. write string contents to a temporary xml file and navigate to it):

//create a random temporary file with an .xml file extension
var path = Path.GetTempPath();
var fileName = Guid.NewGuid().ToString() + ".xml";
var fullFileName = Path.Combine(path, fileName);
//write the contents of your xml string to the temporary file we just created
File.WriteAllText(fullFileName, xmlText); //xmlText is your xml string
//"navigate" to the file
webBrowser.Navigate(fullFileName); //webBrowser is your WebBrowser control
like image 80
Stephen Swensen Avatar answered Oct 09 '22 08:10

Stephen Swensen


There is a good link here: Displaying XML in the .NET WebBrowser Control

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();
  }
}
like image 30
Simon Mourier Avatar answered Oct 09 '22 09:10

Simon Mourier