【发布时间】:2017-04-09 23:49:51
【问题描述】:
美好的一天
我对在 Windows 窗体应用程序中显示 html 文档有疑问。我正在开发的应用程序应该显示来自
的信息html 格式的数据库。我将尝试描述我已经采取的行动(以及失败的行动):
1) 我尝试加载仅存在于内存中的“虚拟”html 页面并动态更改其参数(webbMain 是一个 WebBrowser 控件):
public static string CreateBookHtml()
{
StringBuilder sb = new StringBuilder();
//Declaration
sb.AppendLine(@"<?xml version=""1.0"" encoding=""utf-8""?>");
sb.AppendLine(@"<?xml-stylesheet type=""text/css"" href=""style.css""?>");
sb.AppendLine(@"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.1//EN""
""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"">");
sb.AppendLine(@"<html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"">");
//Head
sb.AppendLine(@"<head>");
sb.AppendLine(@"<title>Exemplary document</title>");
sb.AppendLine(@"<meta http-equiv=""Content-Type"" content=""application/xhtml+xml;
charset=utf-8""/ >");
sb.AppendLine(@"</head>");
//Body
sb.AppendLine(@"<body>");
sb.AppendLine(@"<p id=""paragraph"">Example.</p>");
sb.AppendLine(@"</body>");
sb.AppendLine(@"</html>");
return sb.ToString();
}
void LoadBrowser() { this.webbMain.Navigate("about:blank"); this.webbMain.DocumentText = CreateBookHtml(); HtmlDocument doc = this.webbMain.Document; }
这失败了,因为 doc.Body 为 null,并且 doc.getElementById("paragraph") 也返回 null。所以我不能更改段落 InnerText 属性。
此外,this.webbMain.DocumentText 是“\0”...
2) 我尝试在指定文件夹中创建 html 文件,将其加载到 WebBrowser,然后更改其参数。 Html 和
创建的一样CreateBookHtml() 方法:
private void LoadBrowser()
{
this.webbMain.Navigate("HTML\\BookPage.html"));
HtmlDocument doc = this.webbMain.Document;
}
这次this.webbMain.DocumentText包含从文件中读取的Html数据,但是doc.Body又返回了null,我还是不能使用元素
getByElementId() 方法。当然,当我有文本时,我会尝试使用正则表达式来获取指定的字段,或者可能会使用其他技巧来实现目标,但我想知道 - 是否有简单的方法来维护 html?对我来说,理想的方法是在内存中创建 HTML 文本,将其加载到 WebBrowser 控件中,然后使用 ID 动态更改其参数。可能吗?感谢您提前回答,最好的问候,
帕维尔
【问题讨论】:
-
WebBrowser控件没有NavigateToString方法