|
Hi,
(This is on .NET 2.0)
The only way I managed to show XML in the webbrowser control was to write the xml contents to a file and then use "Navigate(...)" to open the file. This is a bit awkward for me because I have the XML in a string variable, and the most straightforward way to do it would be to simply assign it to the "DocumentText" property. But that did not work... (nothing is displayed, although using the context menu to view the page source shows that my xml content is there).
Any tips on how to do this?
Thanks Rubens
|
| Rubens3 Thursday, September 08, 2005 12:02 AM |
I want to do the exact same thing anyone know how?
Cheers
|
| mlowne Monday, February 13, 2006 10:42 PM |
I want to do the same thing, except I want to show a .pdf. I am
rendering a report through report server in a .pdf format. I can
export this .pdf to a file and then use the .Navigate to view the .pdf.
My next step was to render the report to .pdf but then store it in a
database. I've done that and now I can only get it to work by
loading the pdf from the database, saving it to a file location, and
then navigating to that directly. I don't understand how to take
the byte array and load the browser control with it.
Please post you solution if you find it and I will do the same.
|
| Greg Leepart Wednesday, February 15, 2006 10:02 PM |
I dont think it is possible looking at the architecture of Web Browser.
http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/overview/ie_arch.asp
If any one has done it .. please share |
| Armoghan Thursday, February 16, 2006 1:14 PM |
You cant view XML 'as is' as it has no displayable value. The best way
to display it would be to create an XSLT stylesheet to convert it to
html in the format you want, with something like a
XslCompiledTransform, write the output to a stream and write that
stream to the DocumentText instead.
|
| Woody UK Friday, February 17, 2006 2:05 PM |
Offcourse, i have an XSLT attached to it like
<?xml-stylesheet type="text/xsl" href="a.xsl"?>
on the 2nd line of XML
i have tried transforming via two methods
////////////////////////////////
Private Function getHTML1() As String
Dim fxml As New IO.FileStream("C:\a.xml", IO.FileMode.Open)
Dim myXPathDocument As New Xml.XPath.XPathDocument(fxml)
Dim myXslTransform As New Xml.Xsl.XslCompiledTransform
Dim resultDoc As New IO.MemoryStream
Dim writer As New Xml.XmlTextWriter(resultDoc, Nothing)
myXslTransform.Load( "C:\a.xsl")
myXslTransform.Transform(myXPathDocument, Nothing, writer)
writer.Close()
Dim stream As New IO.StreamReader(resultDoc)
Return stream.ReadToEnd
End Function
/////////////////////////and MSXML old way///////////////
Private Function getHTML2() As String
Dim xmlDoc As New MSXML2.DOMDocument40Class
xmlDoc.load( "C:\a.xml")
Dim xsl As New MSXML2.FreeThreadedDOMDocument40Class
Dim xslt As New MSXML2.XSLTemplate40Class
xsl.load( "C:\a.xsl")
xslt.stylesheet = xsl
xslt.createProcessor()
Dim xslProc As MSXML2.IXSLProcessor = xslt.createProcessor
xslProc.input = xmlDoc
xslProc.transform()
Return xslProc.output.ToString
End Function
/////////////////
But both fail to transform XSL file which is transformed by IEeasily.
At least two problems i have noted.
If XSLstarts with comments
like
<!-- Author Name = XYZ -->
It will not work
And Secondly
Such tags are not working
<xsl:variable name="js_path" select="substring-after(TOP_LEVEL/XSL_SERVER_PATH, ',')" />
while they were workingin IE |
| Armoghan Friday, February 17, 2006 2:42 PM |
following is the code for XSL which is not working
NOTE: I do not want to remove comments and variable
<!--
Version # :: 3.0.1.00
-->
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>Test Exam</title>
<xsl:variable name="display_path" select="substring-before(TOP_LEVEL/XSL_SERVER_PATH, ',')" />
<xsl:variable name="color" select="TOP_LEVEL/XSL_COLOR"/>
</head>
<body>
<xsl:text>Display Path ::</xsl:text>
<xsl:value-of select="$display_path"/>
<br/>
<xsl:text>Color Name ::</xsl:text>
<xsl:value-of select="$color"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XML is
<?xml version="1.0" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="a.xsl"?>
<TOP_LEVEL num="1">
<XSL_SERVER_PATH>C:\Documents and Settings\aasif,C:\\Documents and Settings\\aasif</XSL_SERVER_PATH>
<XSL_COLOR>Cyan</XSL_COLOR>
</TOP_LEVEL> |
| Armoghan Friday, February 17, 2006 3:11 PM |
Hi everyone,
I managed to do this using the following strategy:
1. Download an HTML 1.0 compliant version of defaultss.xsl
2. Create an XMLCompiledTransform object and load the "defaultss.xsl" stylesheet. I actually embedded it is a resource in my assembly and read it from there.
3. Transform your XML stream using the XMLCompiledTransform into an HTML stream.
4. Set the WebBrowser.DocumentStream = HTML stream
Remember to "AllowNavigation" otherwise the thing doesn't work.
Hope this helps,
B. |
| Brian Rogers Friday, April 07, 2006 7:11 PM |
Please see my comment
>>>NOTE: I do not want to remove comments and variable
and I want to use my own Xsl file which is working fine with IE otherwise |
| Armoghan Tuesday, April 11, 2006 11:21 AM |
Hi Armoghan,
This is a display transformation process. Your XML output would be the input to the transformation and the output is a html like IE produces which you display.
B. |
| Brian Rogers Wednesday, April 12, 2006 1:50 PM |
Probably I am unable to convery properly.
You are saying this
XML-->
Transform To HTML ---- Move the resultant to-------> IE
XSL -->
That is what I want to do and doing
But No transformer is like IE's transformer. As I have a complex XML and XSL, for which XSL transformer object I have is not working properly.
See my comments above |
| Armoghan Wednesday, April 12, 2006 2:50 PM |
Just give a try to the XML and XSL I have pasted above.
using your method. It will not work unless you change the XSL
Whereas, drag and drop the XML in IE and it will work fine |
| Armoghan Wednesday, April 12, 2006 2:52 PM |
Hi Armoghan,
Lets assume the following:
You have followed your process and you now have to processed xml you would like to display. The process you want to replicate is displaying xml in the WebBrowser control just like in IE6 when you load an xml file.
To do so, you have to put the xml through ANOTHER transformation using the XSL file I indicated (the SAME one IE6 uses when it displays xml). You take the results of THAT transformation and display it in the WebBrowser control.
Do you understand the process now? |
| Brian Rogers Wednesday, April 12, 2006 2:59 PM |
Hi there, I wanted to do the same thing as you are, and I managed to do it using the following XSLT. Its complex, and hard to customize. But if you are happy with the way of display in IE, then this is going to be the same. Credits have been mentioned within the XSLT itself.
Hope that helps many others looking for this solution as well. BTW, you have to use XsltCompiledTransform.
Cheers, Vinayak
<?xml version="1.0"?> <!-- IE5 default style sheet, provides a view of any XML document and provides the following features: - auto-indenting of the display, expanding of entity references - click or tab/return to expand/collapse - color coding of markup - color coding of recognized namespaces - xml, xmlns, xsl, dt This style sheet is available in IE5 in a compact form at the URL "res://msxml.dll/DEFAULTSS.xsl". This version differs only in the addition of comments and whitespace for readability. Author: Jonathan Marsh (jmarsh@microsoft.com) Modified: 05/21/2001 by Nate Austin (naustin@idsgrp.com) Converted to use XSLT rather than WD-xsl -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dt="urn:schemas-microsoft-com:datatypes" xmlns:d2="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">
<xsl:template match="/"> <HTML> <HEAD> <STYLE> BODY {font:x-small 'Verdana'; margin-right:1.5em} <!-- container for expanding/collapsing content --> .c {cursor:hand} <!-- button - contains +/-/nbsp --> .b {color:red; font-family:'Courier New'; font-weight:bold; text-decoration:none} <!-- element container --> .e {margin-left:1em; text-indent:-1em; margin-right:1em} <!-- comment or cdata --> .k {margin-left:1em; text-indent:-1em; margin-right:1em} <!-- tag --> .t {color:#990000} <!-- tag in xsl namespace --> .xt {color:#990099} <!-- attribute in xml or xmlns namespace --> .ns {color:red} <!-- attribute in dt namespace --> .dt {color:green} <!-- markup characters --> .m {color:blue} <!-- text node --> .tx {font-weight:bold} <!-- multi-line (block) cdata --> .db {text-indent:0px; margin-left:1em; margin-top:0px; margin-bottom:0px; padding-left:.3em; border-left:1px solid #CCCCCC; font:small Courier} <!-- single-line (inline) cdata --> .di {font:small Courier} <!-- DOCTYPE declaration --> .d {color:blue} <!-- pi --> .pi {color:blue} <!-- multi-line (block) comment --> .cb {text-indent:0px; margin-left:1em; margin-top:0px; margin-bottom:0px; padding-left:.3em; font:small Courier; color:#888888} <!-- single-line (inline) comment --> .ci {font:small Courier; color:#888888} PRE {margin:0px; display:inline} </STYLE>
<SCRIPT language="javascript"> <xsl:comment> <![CDATA[ // Detect and switch the display of CDATA and comments from an inline view // to a block view if the comment or CDATA is multi-line. function f(e) { // if this element is an inline comment, and contains more than a single // line, turn it into a block comment. if (e.className == "ci") { if (e.children(0).innerText.indexOf("\n") > 0) fix(e, "cb"); } // if this element is an inline cdata, and contains more than a single // line, turn it into a block cdata. if (e.className == "di") { if (e.children(0).innerText.indexOf("\n") > 0) fix(e, "db"); } // remove the id since we only used it for cleanup e.id = ""; } // Fix up the element as a "block" display and enable expand/collapse on it function fix(e, cl) { // change the class name and display value e.className = cl; e.style.display = "block"; // mark the comment or cdata display as a expandable container j = e.parentElement.children(0); j.className = "c";
// find the +/- symbol and make it visible - the dummy link enables tabbing k = j.children(0); k.style.visibility = "visible"; k.href = "#"; }
// Change the +/- symbol and hide the children. This function workss on "element" // displays function ch(e) { // find the +/- symbol mark = e.children(0).children(0); // if it is already collapsed, expand it by showing the children if (mark.innerText == "+") { mark.innerText = "-"; for (var i = 1; i < e.children.length; i++) e.children(i).style.display = "block"; } // if it is expanded, collapse it by hiding the children else if (mark.innerText == "-") { mark.innerText = "+"; for (var i = 1; i < e.children.length; i++) e.children(i).style.display="none"; } } // Change the +/- symbol and hide the children. This function work on "comment" // and "cdata" displays function ch2(e) { // find the +/- symbol, and the "PRE" element that contains the content mark = e.children(0).children(0); contents = e.children(1); // if it is already collapsed, expand it by showing the children if (mark.innerText == "+") { mark.innerText = "-"; // restore the correct "block"/"inline" display type to the PRE if (contents.className == "db" || contents.className == "cb") contents.style.display = "block"; else contents.style.display = "inline"; } // if it is expanded, collapse it by hiding the children else if (mark.innerText == "-") { mark.innerText = "+"; contents.style.display = "none"; } } // Handle a mouse click function cl() { e = window.event.srcElement; // make sure we are handling clicks upon expandable container elements if (e.className != "c") { e = e.parentElement; if (e.className != "c") { return; } } e = e.parentElement; // call the correct funtion to change the collapse/expand state and display if (e.className == "e") ch(e); if (e.className == "k") ch2(e); }
// Dummy function for expand/collapse link navigation - trap onclick events instead function ex() {}
// Erase bogus link info from the status window function h() { window.status=" "; }
// Set the onclick handler document.onclick = cl; ]]> </xsl:comment> </SCRIPT> </HEAD>
<BODY class="st"> <xsl:apply-templates/> </BODY> </HTML> </xsl:template>
<!-- Templates for each node type follows. The output of each template has a similar structure to enable script to walk the result tree easily for handling user interaction. -->
<!-- Template for the DOCTYPE declaration. No way to get the DOCTYPE, so we just put in a placeholder -->
<!-- no support for doctypes <xsl:template match="node()[nodeType()=10]"> <DIV class="e"><SPAN> <SPAN class="b"> </SPAN> <SPAN class="d"><!DOCTYPE <xsl:value-of select="name()"/><I> (View Source for full doctype...)</I>></SPAN> </SPAN></DIV> </xsl:template> -->
<!-- Template for pis not handled elsewhere --> <xsl:template match="processing-instruction()"> <DIV class="e"> <SPAN class="b"> </SPAN> <SPAN class="m"><?</SPAN> <SPAN class="pi"> <xsl:value-of select="name()"/> <xsl:value-of select="."/> </SPAN> <SPAN class="m">?></SPAN> </DIV> </xsl:template>
<!-- Template for the XML declaration. Need a separate template because the pseudo-attributes are actually exposed as attributes instead of just element content, as in other pis --> <!-- No support for the xml declaration <xsl:template match="pi('xml')"> <DIV class="e"> <SPAN class="b"> </SPAN> <SPAN class="m"><?</SPAN><SPAN class="pi">xml <xsl:for-each select="@*"><xsl:value-of select="name()"/>="<xsl:value-of select="."/>" </xsl:for-each></SPAN><SPAN class="m">?></SPAN> </DIV> </xsl:template> -->
<!-- Template for attributes not handled elsewhere --> <xsl:template match="@*" xml:space="preserve"><SPAN><xsl:attribute name="class"><xsl:if test="starts-with(name(),'xsl:')">x</xsl:if>t</xsl:attribute> <xsl:value-of select="name()" /></SPAN><SPAN class="m">="</SPAN><B><xsl:value-of select="."/></B><SPAN class="m">"</SPAN></xsl:template>
<!-- Template for attributes in the xmlns or xml namespace --> <!-- UNKNOWN <xsl:template match="@xmlns:*|@xmlns|@xml:*"><SPAN class="ns"> <xsl:value-of select="name()"/></SPAN><SPAN class="m">="</SPAN> <B class="ns"><xsl:value-of select="."/></B><SPAN class="m">"</SPAN></xsl:template> -->
<!-- Template for attributes in the dt namespace --> <!-- UNKNOWN <xsl:template match="@dt:*|@d2:*"><SPAN class="dt"> <xsl:value-of select="name()"/></SPAN><SPAN class="m">="</SPAN><B class="dt"><xsl:value-of select="."/></B><SPAN class="m">"</SPAN></xsl:template> -->
<!-- Template for text nodes --> <xsl:template match="text()"> <DIV class="e"> <SPAN class="b"> </SPAN> <SPAN class="tx"> <xsl:value-of select="."/> </SPAN> </DIV> </xsl:template>
<!-- Note that in the following templates for comments and cdata, by default we apply a style appropriate for single line content (e.g. non-expandable, single line display). But we also inject the attribute 'id="clean"' and a script call 'f(clean)'. As the output is read by the browser, it executes the function immediately. The function checks to see if the comment or cdata has multi-line data, in which case it changes the style to a expandable, multi-line display. Performing this switch in the DHTML instead of from script in the XSL increases the performance of the style sheet, especially in the browser's asynchronous case -->
<!-- Template for comment nodes --> <xsl:template match="comment()"> <DIV class="k"> <SPAN> <A class="b" onclick="return false" onfocus="h()" STYLE="visibility:hidden">-</A> <SPAN class="m"><!--</SPAN> </SPAN> <SPAN id="clean" class="ci"> <PRE> <xsl:value-of select="."/> </PRE> </SPAN> <SPAN class="b"> </SPAN> <SPAN class="m">--></SPAN> <SCRIPT>f(clean);</SCRIPT> </DIV> </xsl:template>
<!-- Template for cdata nodes --> <!-- UNSUPPORTED <xsl:template match="cdata()"> <DIV class="k"> <SPAN><A class="b" onclick="return false" onfocus="h()" STYLE="visibility:hidden">-</A> <SPAN class="m"> <![CDATA[</SPAN></SPAN> <SPAN id="clean" class="di"><PRE><xsl:value-of select="."/></PRE></SPAN> <SPAN class="b"> </SPAN> <SPAN class="m">]]></SPAN> <SCRIPT>f(clean);</SCRIPT></DIV> </xsl:template> -->
<!-- Note the following templates for elements may examine children. This harms to some extent the ability to process a document asynchronously - we can't process an element until we have read and examined at least some of its children. Specifically, the first element child must be read before any template can be chosen. And any element that does not have element children must be read completely before the correct template can be chosen. This seems an acceptable performance loss in the light of the formatting possibilities available when examining children. -->
<!-- Template for elements not handled elsewhere (leaf nodes) --> <xsl:template match="*"> <DIV class="e"> <DIV STYLE="margin-left:1em;text-indent:-2em"> <SPAN class="b"> </SPAN> <SPAN class="m"><</SPAN> <SPAN> <xsl:attribute name="class"> <xsl:if test="starts-with(name(),'xsl:')">x</xsl:if>t </xsl:attribute> <xsl:value-of select="name()"/> </SPAN> <xsl:apply-templates select="@*"/> <SPAN class="m">/></SPAN> </DIV> </DIV> </xsl:template>
<!-- Template for elements with comment, pi and/or cdata children --> <xsl:template match="*[comment() | processing-instruction()]"> <DIV class="e"> <DIV class="c"> <A href="#" onclick="return false" onfocus="h()" class="b">-</A> <SPAN class="m"><</SPAN> <SPAN> <xsl:attribute name="class"> <xsl:if test="starts-with(name(),'xsl:')">x</xsl:if>t </xsl:attribute> <xsl:value-of select="name()"/> </SPAN> <xsl:apply-templates select="@*"/> <SPAN class="m">></SPAN> </DIV> <DIV> <xsl:apply-templates/> <DIV> <SPAN class="b"> </SPAN> <SPAN class="m"></</SPAN> <SPAN> <xsl:attribute name="class"> <xsl:if test="starts-with(name(),'xsl:')">x</xsl:if>t </xsl:attribute> <xsl:value-of select="name()"/> </SPAN> <SPAN class="m">></SPAN> </DIV> </DIV> </DIV> </xsl:template>
<!-- Template for elements with only text children --> <xsl:template match="*[text() and not(comment() | processing-instruction())]"> <DIV class="e"> <DIV STYLE="margin-left:1em;text-indent:-2em"> <SPAN class="b"> </SPAN> <SPAN class="m"><</SPAN> <SPAN> <xsl:attribute name="class"> <xsl:if test="starts-with(name(),'xsl:')">x</xsl:if>t </xsl:attribute> <xsl:value-of select="name()"/> </SPAN> <xsl:apply-templates select="@*"/> <SPAN class="m">></SPAN> <SPAN class="tx"> <xsl:value-of select="."/> </SPAN> <SPAN class="m"></</SPAN> <SPAN> <xsl:attribute name="class"> <xsl:if test="starts-with(name(),'xsl:')">x</xsl:if>t </xsl:attribute> <xsl:value-of select="name()"/> </SPAN> <SPAN class="m">></SPAN> </DIV> </DIV> </xsl:template>
<!-- Template for elements with element children --> <xsl:template match="*"> <DIV class="e"> <DIV class="c" STYLE="margin-left:1em;text-indent:-2em"> <A href="#" onclick="return false" onfocus="h()" class="b">-</A> <SPAN class="m"><</SPAN> <SPAN> <xsl:attribute name="class"> <xsl:if test="starts-with(name(),'xsl:')">x</xsl:if>t </xsl:attribute> <xsl:value-of select="name()"/> </SPAN> <xsl:apply-templates select="@*"/> <SPAN class="m">></SPAN> </DIV> <DIV> <xsl:apply-templates/> <DIV> <SPAN class="b"> </SPAN> <SPAN class="m"></</SPAN> <SPAN> <xsl:attribute name="class"> <xsl:if test="starts-with(name(),'xsl:')">x</xsl:if>t </xsl:attribute> <xsl:value-of select="name()"/> </SPAN> <SPAN class="m">></SPAN> </DIV> </DIV> </DIV> </xsl:template>
</xsl:stylesheet>
|
| Vinayak Kamat Friday, April 14, 2006 8:31 PM |
Hi .. i have implemented the same bit. ie. showing PDF/ HTML/ TEXT file in the webbrowser.
I implemented that as i was saving all my files in the database as byte array ... once i retrieve all the bytes on the client. I converted them into physical file and use the webbrowser navigate and pass the entire path of any newly created file.
Thanks
|
| R.A.F Tuesday, February 20, 2007 4:16 PM |
Using .Net 2.0, all you have to do is get the data into a stream, then set the browser document stream property. There is no longer a requirement to save to the disk. |
| Brian Rogers Thursday, February 22, 2007 2:29 PM |
hi, gleepart,
I have a little project trying to take the rendered .pdf to a stream and either save it to a file system or send it out in an email. The .pdf file was generated by a Business Objects report engine and was created from several javascripts as I can tell.
Since I saw you were able to get it though report server, can you show some light as how you accomplish it?
Appreciate it. |
| jamesbin Sunday, April 01, 2007 3:13 PM |
I've succeeded in getting raw XML shaped and output to the webBrowser control by using defaultss.xslt
The problem I have is to get the webBrowser control to display other document types In particular at the moment I'm interested in PDF and SVG, but I expect to also need to display vrml and maybe others too. My current system is that there is client xml data from a database, and for any given data there may be xslt data describing how it should be converted to it's final form. When I save the XML as an XML file, and the XSLT as an XSLT file, I can display the results correctly. (so far I've been testing with SVG final output). In other words, passing the browser control the XML and XSLT gets me a display of the SVG image I expect to see.
If I handle the SVG parsing in code and pass the result to documentText (or documentStream) I don't get any recognition of the doctype. I've tried passing the result through the defaultss.xslt but that just pretty-prints it as xml.
How can I get doc-type info honoured by the browser without having to save the files of externally?
|
| Keith Ealanta Sunday, September 02, 2007 1:09 PM |
Check this..http://blorgh.wordpress.com/2006/10/17/displaying-xml-in-the-net-webbrowser-control/
it works fine...if not let me know..
Happy coding
|
| Kiran Patil Tuesday, October 16, 2007 6:55 AM |
Hi,
i am displaying a pdf in web Browser as well. i want to save it on disk . Did you find any workable solution to this problem. Any help will be appreciated.
|
| Kamal109 Friday, October 19, 2007 6:54 PM |
I am using defaultSS.xml in the webbrowser control, but it is removing the spaces between attributes.
This does not happen in IE.
Any ideas what I am doing wrong ? |
| RossAu42 Monday, November 05, 2007 5:45 AM |
I loaded this XSLT stylesheet into XslCompiledTransform as suggested. If I use the overloaded Transform method Transform(string xmlPath, string outputPath) it gives me what I would expect, HTML that can be loaded into a WebBrowser control. However if I load the XML into XmlDocument using LoadXml and create an XmlWriter using XmlWriter writer = XmlWriter.Create("....") then use Transform(doc, writer) it doesn't seem to transform the data . I see what appears to be HTML in the output file but there is alot missing and it doesn't display correctly in the WebBrowser. Any ideas on the difference?
Thank you.
Kevin
|
| KevinBurton Friday, January 18, 2008 8:45 PM |
Hi there,
perhaps you might want to try WebBrowser.DocumentStream and write your data there...
Cheers. |
| Fernando Gómez Tuesday, September 08, 2009 4:05 PM |
Better way is to skip the WB control entirely, just download the source directly. /// <summary> /// Retrieve the source code of a website, skip the web browser control. /// </summary> /// <param name="sourceUrl"></param> /// <returns></returns> public static string GetSourceCode(string sURL) { WebClient wClient = new WebClient(); wClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0;)"); // pass as Internet Explorer 7.0 Stream sData = wClient.OpenRead(sURL); StreamReader srReader = new StreamReader(sData); string sBuffer = srReader.ReadToEnd(); sData.Close(); srReader.Close(); return sBuffer; } Code from a thread on stackoverflow.com http://stackoverflow.com/questions/934192/how-do-i-save-xml-in-a-webbrowser-control |
| JaredBroad Sunday, September 13, 2009 11:52 PM |