howto convert xml to html using xsl and display result in WebBrowser control

Hi,

I'm trying to display some html in a WebBrowser control. The html should be built by transforming an xml string using an xsl file. This is the code I use:

System.Xml.Xsl.XslCompiledTransform transformer = new System.Xml.Xsl.XslCompiledTransform();
transformer.Load("C:\\XSL\\" + xsl + ".xsl");
System.IO.MemoryStream ms = new System.IO.MemoryStream();
// reader is an XMLReader that is filled using a stored procedure
transformer.Transform(reader, null, ms);
contentBrowser.DocumentStream = ms;


It seems that after the transformation, the MemoryStream only contains
i   

The XMLReader contains the data though, because when I write it to a file and navigate to it, it's displaying fine. However, I don't want the overhead of having to create a temp file every time I need to display.

string content = string.Empty;
while (reader.Read())
content += reader.ReadOuterXml();
reader.Close();

System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\test.xml");
sw.Write("< xml version=\"1.0\" encoding=\"ISO-8859-1\" >< xml-stylesheet href=\"" +
"C:\\XSL\\" xsl + ".xsl\" type=\"text/xsl\" ><report>" + content + "</report>");
sw.Close();


So to me it seems that it's the transformation that fails somehow. There are no exceptions though. Any thoughts on how this can be resolved ,

Cheers,

Phil


Answer this question

howto convert xml to html using xsl and display result in WebBrowser control

  • dparvin

    Are you shure that your XSLT produce any output

    Three bytes you see can be UTF-8 byte order mark.



  • Eric Sch.

    Phil,

    could you try

    ms.Position = 0;

    before setting it to contentBrowser

    Sinan



  • saiwa

    Hi Sinan,

    unfortunately, that doesn't work either. The thing is that the Length property of the MemoryStream returns 3, which means that all it contains are the three strange characters. So for me, the transform function does not fill the stream.

    Any other suggestions

    thanks,
    Phil

  • sjma

    Do not use memorystream directly, wrap a textwriter over it.

    MemoryStream ms = new MemoryStream();
    StreamWriter sw = new StreamWriter(ms);

    xsl.Transform(input, null, sw);

    XmlDocument page = new XmlDocument();
    byte[] bytes = ms.ToArray();
    string transformedXml = Encoding.UTF8.GetString(bytes);
    page.LoadXml(transformedXml);
    sw.Dispose();
    ms.Close();
    ms.Dispose();



  • bkee

    I actually didn't test that code.

    I just tested this code. it transforms and reads from memorystream.

    System.Xml.Xsl.XslCompiledTransform transformer = new System.Xml.Xsl.XslCompiledTransform();

    transformer.Load("style.xslt");

    MemoryStream ms = new MemoryStream();

    StreamWriter sw = new StreamWriter(ms);

    transformer.Transform("plant.xml", null, sw); //file to load

    XmlDocument page = new XmlDocument();

    byte[] bytes = ms.ToArray();

    string transformedXml = Encoding.UTF8.GetString(bytes);

    page.LoadXml(transformedXml);

    sw.Dispose();

    ms.Close();

    ms.Dispose();



  • Jerome.Robert

    Sinan,

    Doing it this way causes the Length of the MemoryStream to be 0. Did you test the code from your previous post yourself If so, what did you use as input Is it an XMLReader like I'm using

    cheers,
    Phil

  • howto convert xml to html using xsl and display result in WebBrowser control