我有一個string
,其中包含一個xml
。我想用它設置WebBrowser控件的值,並顯示爲xml
。如何使用WebBrowser控件顯示XML?
我可以使用browser.DocumentText
來設置該值,但是如何告訴它顯示爲XML?
我有一個string
,其中包含一個xml
。我想用它設置WebBrowser控件的值,並顯示爲xml
。如何使用WebBrowser控件顯示XML?
我可以使用browser.DocumentText
來設置該值,但是如何告訴它顯示爲XML?
不是特別容易,但可能。您可以將字符串保存在xml文件中,然後使用Navigate()(yuck ...)加載它,或者將xsl應用到您的xml(不要開玩笑!),它可以像IE瀏覽器一樣渲染它。
看到:
http://social.msdn.microsoft.com/Forums/en/winforms/thread/18ca6734-ddb7-4f44-a77c-9a7713dcc2e1
有一個很好的鏈接在這裏: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();
}
}
添加相關的代碼,以防萬一鏈接死亡。 – 2015-03-24 22:28:30
給一些代碼加入到第一溶液@PaoloFalabella建議(即寫入字符串內容到一個臨時的XML文件並導航到它):
//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
這隻會顯示xml內部內容,但不會顯示整個xml標籤 – 2015-07-02 10:02:48
@ nicolas2008我不完全確定您聲稱的是什麼,但我在這裏只使用這種方法休息(http://www.swensensoftware.com/im-只是休息),它沒有問題(完整的XML文檔顯示就像IE顯示任何XML文檔)。 – 2015-09-22 16:08:27
工程很好。只有這樣才能將文本寫入文件中傳遞,如<?xml ... encoding =「...」>>'中所聲明的匹配'Encoding',以便它顯示(否則'WebBrowser'會顯示錯誤頁面)。 – Loathing 2015-10-30 03:08:02
在這裏,我提供了一個分步解決方案來顯示WebBrowser控件內的XML文件。
並複製下面的方法。
private void DisplayXml()
{
string xmlString = "<Person><Name>Fawad</Name><Age>23</Age></Person>";
// Load the xslt used by IE to render the xml
XslCompiledTransform xTrans = new XslCompiledTransform();
xTrans.Load(Path.Combine(new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName, @"resources\defaultss.xsl"));
// Read the xml string.
StringReader sr = new StringReader(xmlString);
XmlReader xReader = XmlReader.Create(sr);
// Transform the XML data
MemoryStream ms = new MemoryStream();
xTrans.Transform(xReader, null, ms);
ms.Position = 0;
// Set to the document stream
webBrowser1.DocumentStream = ms;
}
我複製「defaultss.xsl」在這裏爲那些誰也找不到它的任何地方的內容。只需簡單地將它放在記事本文件中,然後將其保存爲xsl格式,並將其命名爲「resources」即可。
<!--
|
| XSLT REC Compliant Version of IE5 Default Stylesheet
|
| Original version by Jonathan Marsh ([email protected])
| http://msdn.microsoft.com/xml/samples/defaultss/defaultss.xsl
|
| Conversion to XSLT 1.0 REC Syntax by Steve Muench ([email protected])
|
+-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Add doctype attributes to keep IE happy -->
<xsl:output indent="no"
method="html"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd" />
<xsl:template match="/">
<HTML>
<HEAD>
<SCRIPT>
<xsl:comment>
<![CDATA[
function f(e){
if (e.className=="ci") {
if (e.children(0).innerText.indexOf("\n")>0) fix(e,"cb");
}
if (e.className=="di") {
if (e.children(0).innerText.indexOf("\n")>0) fix(e,"db");
} e.id="";
}
function fix(e,cl){
e.className=cl;
e.style.display="block";
j=e.parentElement.children(0);
j.className="c";
k=j.children(0);
k.style.visibility="visible";
k.href="#";
}
function ch(e) {
mark=e.children(0).children(0);
if (mark.innerText=="+") {
mark.innerText="-";
for (var i=1;i<e.children.length;i++) {
e.children(i).style.display="block";
}
}
else if (mark.innerText=="-") {
mark.innerText="+";
for (var i=1;i<e.children.length;i++) {
e.children(i).style.display="none";
}
}
}
function ch2(e) {
mark=e.children(0).children(0);
contents=e.children(1);
if (mark.innerText=="+") {
mark.innerText="-";
if (contents.className=="db"||contents.className=="cb") {
contents.style.display="block";
}
else {
contents.style.display="inline";
}
}
else if (mark.innerText=="-") {
mark.innerText="+";
contents.style.display="none";
}
}
function cl() {
e=window.event.srcElement;
if (e.className!="c") {
e=e.parentElement;
if (e.className!="c") {
return;
}
}
e=e.parentElement;
if (e.className=="e") {
ch(e);
}
if (e.className=="k") {
ch2(e);
}
}
function ex(){}
function h(){window.status=" ";}
document.onclick=cl;
]]>
</xsl:comment>
</SCRIPT>
<STYLE>
BODY {font:small 'Verdana'; margin-right:1.5em; margin-top: 44px;}
.c {cursor:hand}
.b {color:red; font-family:'Courier New'; font-weight:bold;
text-decoration:none}
.e {margin-left:1em; text-indent:-1em; margin-right:1em}
.k {margin-left:1em; text-indent:-1em; margin-right:1em}
.t {color:#990000}
.xt {color:#990099}
.ns {color:red}
.dt {color:green}
.m {color:blue}
.tx {font-weight:bold}
.db {text-indent:0px; margin-left:1em; margin-top:0px;
margin-bottom:0px;padding-left:.3em;
border-left:1px solid #CCCCCC; font:x-small Courier}
.di {font:x-small Courier}
.d {color:blue}
.pi {color:blue}
.cb {text-indent:0px; margin-left:1em; margin-top:0px;
margin-bottom:0px;padding-left:.3em; font:x-small Courier;
color:#888888}
.ci {font:x-small Courier; color:#888888}
PRE {margin:0px; display:inline}
.label {padding-left:20px; vertical-align: middle}
.validation {color: white; padding: 3px; margin: 5px 5px 5px 5px; text-indent: 0}
.summary {position: fixed; top: 0; left: 0; margin: 0px; padding-top: 10px; width: 100%; height: 32px; font-size: 12pt; vertical-align: middle;border-bottom: 2px solid black}
.nav {float: right; padding-right:20px;}
.failure {background: red;}
.success {background: green;}
.warning {background: yellow; color: black}
.selected {font-weight: bold; text-indent: 1em}
</STYLE>
</HEAD>
<BODY class="st">
<xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>
<!-- Render the schema summary
Include jquery from CDN and render a title bar across top -->
<xsl:template match="processing-instruction('schemaSummary')">
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<div id="schemaSummary" class="validation summary">
<span class="label">
<xsl:value-of select="." />
</span>
<span class="nav">
<button id="prev"><<</button>
<button id="next">>></button>
</span>
</div>
</xsl:template>
<!-- Handle the schemaValid processing instruction
This sets up a document.ready routine that
colour codes a visual queue i.e. a green title bar.
-->
<xsl:template match="processing-instruction('schemaValid')">
<script>
$(document).ready(function() {
$("#schemaSummary").addClass("success")
$("button").css("display", "none");
});
</script>
</xsl:template>
<!-- Handle the schemaInvalid processing instruction
Renders a red title bar with navigation controls to
step through the errors.
-->
<xsl:template match="processing-instruction('schemaInvalid')">
<script>
<![CDATA[
var index = -1;
var errors = $("div.failure");
var offsetFromTop = $("#schemaSummary").outerHeight();
function nextError(){
if(index > -1)
$(errors).eq(index).removeClass("selected");
index ++;
if(index >= $(errors).size())
index = 0;
$(errors).eq(index).addClass("selected");
scrollTo($(errors).eq(index), offsetFromTop);
}
function prevError(){
$(errors).eq(index).removeClass("selected");
index --;
if(index < 0)
index = $(errors).size() - 1;
$(errors).eq(index).addClass("selected");
scrollTo($(errors).eq(index), offsetFromTop);
}
function scrollTo(element, offsetFromTop) {
$('html,body').animate({scrollTop: $(element).offset().top - offsetFromTop},'fast');
}
$(document).ready(function() {
$("#schemaSummary").addClass("warning");
$("#next").click(function() {
nextError();
});
$("#prev").click(function() {
prevError();
});
});
]]>
</script>
</xsl:template>
<!-- Add a colour coded bar in situ i.e. where the validation
error has occured -->
<xsl:template match="processing-instruction('error')">
<div class="validation failure">
<xsl:value-of select="."></xsl:value-of>
</div>
</xsl:template>
<xsl:template match="processing-instruction()">
<DIV class="e">
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">lt</xsl:with-param>
</xsl:call-template>?
</SPAN>
<SPAN class="pi">
<xsl:value-of select="name(.)"/>
</SPAN>
<SPAN>
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
<xsl:value-of select="."/>
</SPAN>
<SPAN class="m">
<xsl:text>?></xsl:text>
</SPAN>
</DIV>
</xsl:template>
<xsl:template match="processing-instruction('xml')">
<DIV class="e">
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">lt</xsl:with-param>
</xsl:call-template>?
</SPAN>
<SPAN class="pi">
<xsl:text>xml </xsl:text>
<xsl:for-each select="@*">
<xsl:value-of select="name(.)"/>
<xsl:text>="</xsl:text>
<xsl:value-of select="."/>
<xsl:text>" </xsl:text>
</xsl:for-each>
</SPAN>
<SPAN class="m">
<xsl:text>?></xsl:text>
</SPAN>
</DIV>
</xsl:template>
<xsl:template match="@*">
<SPAN>
<xsl:attribute name="class">
<xsl:if test="xsl:*/@*">
<xsl:text>x</xsl:text>
</xsl:if>
<xsl:text>t</xsl:text>
</xsl:attribute>
<xsl:value-of select="name(.)"/>
</SPAN>
<SPAN class="m">="</SPAN>
<B>
<xsl:value-of select="."/>
</B>
<SPAN class="m">"</SPAN>
</xsl:template>
<xsl:template match="text()">
<DIV class="e">
<SPAN class="b"> </SPAN>
<SPAN class="tx">
<xsl:value-of select="."/>
</SPAN>
</DIV>
</xsl:template>
<xsl:template match="comment()">
<DIV class="k">
<SPAN>
<A STYLE="visibility:hidden" class="b" onclick="return false" onfocus="h()">-</A>
<SPAN class="m">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">lt</xsl:with-param>
</xsl:call-template>!--
</SPAN>
</SPAN>
<SPAN class="ci" id="clean">
<PRE>
<xsl:value-of select="."/>
</PRE>
</SPAN>
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m">
<xsl:text>--></xsl:text>
</SPAN>
<SCRIPT>f(clean);</SCRIPT>
</DIV>
</xsl:template>
<xsl:template match="*">
<DIV class="e">
<DIV STYLE="margin-left:1em;text-indent:-2em">
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">lt</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN>
<xsl:attribute name="class">
<xsl:if test="xsl:*">
<xsl:text>x</xsl:text>
</xsl:if>
<xsl:text>t</xsl:text>
</xsl:attribute>
<xsl:value-of select="name(.)"/>
<xsl:if test="@*">
<xsl:text> </xsl:text>
</xsl:if>
</SPAN>
<xsl:apply-templates select="@*"/>
<SPAN class="m">
<xsl:text>/></xsl:text>
</SPAN>
</DIV>
</DIV>
</xsl:template>
<xsl:template match="*[node()]">
<DIV class="e">
<DIV class="c">
<A class="b" href="#" onclick="return false" onfocus="h()">-</A>
<SPAN class="m">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">lt</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN>
<xsl:attribute name="class">
<xsl:if test="xsl:*">
<xsl:text>x</xsl:text>
</xsl:if>
<xsl:text>t</xsl:text>
</xsl:attribute>
<xsl:value-of select="name(.)"/>
<xsl:if test="@*">
<xsl:text> </xsl:text>
</xsl:if>
</SPAN>
<xsl:apply-templates select="@*"/>
<SPAN class="m">
<xsl:text>></xsl:text>
</SPAN>
</DIV>
<DIV>
<xsl:apply-templates/>
<DIV>
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">lt</xsl:with-param>
</xsl:call-template>?/
</SPAN>
<SPAN>
<xsl:attribute name="class">
<xsl:if test="xsl:*">
<xsl:text>x</xsl:text>
</xsl:if>
<xsl:text>t</xsl:text>
</xsl:attribute>
<xsl:value-of select="name(.)"/>
</SPAN>
<SPAN class="m">
<xsl:text>></xsl:text>
</SPAN>
</DIV>
</DIV>
</DIV>
</xsl:template>
<xsl:template match="*[text() and not (comment() or processing-instruction())]">
<DIV class="e">
<DIV STYLE="margin-left:1em;text-indent:-2em">
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">lt</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN>
<xsl:attribute name="class">
<xsl:if test="xsl:*">
<xsl:text>x</xsl:text>
</xsl:if>
<xsl:text>t</xsl:text>
</xsl:attribute>
<xsl:value-of select="name(.)"/>
<xsl:if test="@*">
<xsl:text> </xsl:text>
</xsl:if>
</SPAN>
<xsl:apply-templates select="@*"/>
<SPAN class="m">
<xsl:text>></xsl:text>
</SPAN>
<SPAN class="tx">
<xsl:value-of select="."/>
</SPAN>
<SPAN class="m">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">lt</xsl:with-param>
</xsl:call-template>/
</SPAN>
<SPAN>
<xsl:attribute name="class">
<xsl:if test="xsl:*">
<xsl:text>x</xsl:text>
</xsl:if>
<xsl:text>t</xsl:text>
</xsl:attribute>
<xsl:value-of select="name(.)"/>
</SPAN>
<SPAN class="m">
<xsl:text>></xsl:text>
</SPAN>
</DIV>
</DIV>
</xsl:template>
<xsl:template match="*[*]" priority="20">
<DIV class="e">
<DIV STYLE="margin-left:1em;text-indent:-2em" class="c">
<A class="b" href="#" onclick="return false" onfocus="h()">-</A>
<SPAN class="m">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">lt</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN>
<xsl:attribute name="class">
<xsl:if test="xsl:*">
<xsl:text>x</xsl:text>
</xsl:if>
<xsl:text>t</xsl:text>
</xsl:attribute>
<xsl:value-of select="name(.)"/>
<xsl:if test="@*">
<xsl:text> </xsl:text>
</xsl:if>
</SPAN>
<xsl:apply-templates select="@*"/>
<SPAN class="m">
<xsl:text>></xsl:text>
</SPAN>
</DIV>
<DIV>
<xsl:apply-templates/>
<DIV>
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">lt</xsl:with-param>
</xsl:call-template>/
</SPAN>
<SPAN>
<xsl:attribute name="class">
<xsl:if test="xsl:*">
<xsl:text>x</xsl:text>
</xsl:if>
<xsl:text>t</xsl:text>
</xsl:attribute>
<xsl:value-of select="name(.)"/>
</SPAN>
<SPAN class="m">
<xsl:text>></xsl:text>
</SPAN>
</DIV>
</DIV>
</DIV>
</xsl:template>
<xsl:template name="entity-ref">
<xsl:param name="name"/>
<xsl:text disable-output-escaping="yes">&</xsl:text>
<xsl:value-of select="$name"/>
<xsl:text>;</xsl:text>
</xsl:template>
</xsl:stylesheet>
你並不需要保存臨時文件,只是這樣做......
browserControl.NavigateToString(xml);
這是WPF我猜 – user1249190 2017-10-09 09:23:23
你想讓它顯示爲IE瀏覽器(使用XSL樣式表顯示分層視圖)或只是簡單的XML文本? – 2011-03-01 17:18:07
@Simon:就像IE一樣。對於純文本,我可以將其放在「TextBox」中,使用WebBrowser控件沒有任何意義。 – BrunoLM 2011-03-01 17:21:31