我有一个包含xml
的string
。我想用它设置WebBrowser控件的值并显示为xml
。
我可以用browser.DocumentText
设置值,但是如何让它显示为XML呢?
发布于 2011-03-02 01:09:05
这不是特别容易,但也是可能的。要么将字符串保存在xml文件中,然后使用Navigate() (yuck...)重新加载它,要么将xsl应用于xml (不是开玩笑!)这就像IE浏览器一样呈现它。
请参见:
http://social.msdn.microsoft.com/Forums/en/winforms/thread/18ca6734-ddb7-4f44-a77c-9a7713dcc2e1
发布于 2012-09-03 22:53:04
要为@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
发布于 2011-03-02 01:54:32
这里有一个很好的链接: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();
}
}
https://stackoverflow.com/questions/5157545
复制相似问题