LuckyDice
LuckyDice

Reputation: 55

display a xml within a web browser control without XSL

I have a Windows form with a web browser control. When a button is clicked a FileBrowserDialog opens and the user can selects a XML- file. I am now displaying this XML-file with webBrowser1.Navigate(FileBrowserDialog1);.

The problem is that when a XSL-file is declared inside the XML-file it showed the XML-file with the layout of the XSL file.

I am looking for a way to display a xml file without the XSL file.

Upvotes: 2

Views: 3785

Answers (4)

Bora
Bora

Reputation: 291

You need to parse the XML and remove the line with the processing instruction.

Alternatives:

  • Read as text, use Regex.
  • Read as XML, process via XslCompiledTransform and filter out the processing-instruction node. I just tested an example

    <xsl:template match="/">
        <xsl:apply-templates />
    </xsl:template>
    
    <xsl:template match="processing-instruction('xml-stylesheet')"/>
    <xsl:template match="@*|node()|comment()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()|comment()"/>
        </xsl:copy>            
    </xsl:template>
    

should transform a document like this:

    <?xml version='1.0'?>
    <?xml-stylesheet href="dontwant.xsl" type="text/xsl"?>
    <root>
        <!-- comments should stay in the output -->
        <node1>
            <node11></node11>
        </node1>
        <node2/>
    </root>

into this:

<?xml version="1.0" encoding="utf-8"?><root>
    <!-- comments should stay in the output -->
    <node1>
        <node11 />
    </node1>
    <node2 />
</root>

Upvotes: 1

Flynn1179
Flynn1179

Reputation: 12075

NB: This doesn't work, don't do it.

I had suggested trying

webBrowser1.DocumentText = File.ReadAllText(FileBrowserDialog1);

In the hope that it'll load the XML document as a string, and transfer it 'as-is' to the web browser control. Unfortunately, it doesn't actually show you the XML, it tries to treat it as HTML.

Upvotes: 0

Tao
Tao

Reputation: 14006

You could transform your Xml into HTML using a standard Xml-to-Html stylesheet, and display the resulting HTML in the WebBrowser control. Example XSLT to achieve this here: http://www2.informatik.hu-berlin.de/~obecker/XSLT/

The advantage of this approach is that you have full control over the styling of the Xml as displayed in your control. The disadvantage is that the styling most likely won't match the standard IE styling on Xml documents (if that was what you were looking for).

One more note: to display custom html (the HTML generated by the standard XSLT, in this case), you'll have to make some minor modifications to the WebBrowser control. discussion/overview here, and a simple class that achieves this (from another project of mine) here.

Upvotes: 0

Oded
Oded

Reputation: 499112

If you view the source of the page you will get the original XML.

Embedding the stylesheet in the XML file will always cause the browser to transform and display the transformation - there is no way to work around that.

Upvotes: 1

Related Questions