我能找到的唯一解决方案是使用:
mshtml.HTMLDocument htmldocu = new mshtml.HTMLDocument();
htmldocu .createDocumentFromUrl(url, "");
而且我不确定性能,它应该比在 WebBrowser 中加载 html 文件然后从那里获取 HtmlDocument 更好。无论如何,该代码在我的机器上不起作用。应用程序在尝试执行第二行时崩溃。
有没有人可以有效地或以其他方式实现这一点?
注意:请理解我需要 HtmlDocument 对象来处理 DOM。我不需要 html 字符串。
最佳答案
使用 WebClient
对象的 DownloadString
方法。例如
WebClient client = new WebClient();
string reply = client.DownloadString("http://www.google.com");
在上面的示例中,执行后,reply
将包含端点 http://www.google.com
的 html 标记。
关于c# - 在不使用 WebBrowser 或 HAP 的情况下将字符串或 html 文件转换为 C# HtmlDocument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9365832/