vb.net 将字符串转换为 HTML 文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24458250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting a string to an HTML document
提问by user3781458
is there a way to put a string variable into a new declared htmldocument variable without going through a webbrowser ? i tried this
有没有办法在不通过网络浏览器的情况下将字符串变量放入新声明的 htmldocument 变量中?我试过这个
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim v As New WebClient
Dim page As String = ""
Dim ahtm As HtmlDocument = Nothing
page = v.DownloadString("http://www.google.com")
ahtm.Body.InnerText = page 'not working
ahtm.Write(page) 'not working neither
End Sub
采纳答案by Aelphaeis
The reason is because you declared ahtm as Nothing. instantiate it and see if it works.
原因是因为您将 ahtm 声明为 Nothing。实例化它,看看它是否有效。
Update: HtmlDocument is a wrapper around an unmanaged class (IHtmlDocument). Try Declaring a WebBrower and then assigning the ahtm to the web browser document property.
更新:HtmlDocument 是一个非托管类 (IHtmlDocument) 的包装器。尝试声明一个 WebBrower,然后将 ahtm 分配给 Web 浏览器文档属性。
WebBrowser wb = new WebBrowser();
HtmlDocument atm = wb.Document;
In other words, Web browser is the easiest way.
换句话说,Web 浏览器是最简单的方法。
Update: The alternative would be to use something like HtmlAgilityPack. http://htmlagilitypack.codeplex.com/
更新:另一种方法是使用 HtmlAgilityPack 之类的东西。http://htmlagilitypack.codeplex.com/
回答by aban-developer
You can use WebBrowser without Navigate().
您可以在没有 Navigate() 的情况下使用 WebBrowser。
Public Function CreateDocument(ByVal url As String) As HtmlDocument
Dim wb As New WebBrowser()
wb.DocumentText=New WebClient().DownloadString(url)
Return wb.Document
End Function
回答by Tyler Montney
Make sure to add a reference to Microsoft.mshtml.
确保添加对Microsoft.mshtml.
Private Function GetStatus(ByVal HTMLString As String) As mshtml.HTMLDocumentClass
Dim htmlDocument As mshtml.IHTMLDocument2 = New mshtml.HTMLDocumentClass()
htmlDocument.clear()
htmlDocument.write(HTMLString)
htmlDocument.close()
Return htmlDocument
End Function
You very well could try the HTMLAgilitypack, but that's third party. I feel it's better to attempt using what's built in (unless third party is vastly superior in functionality and/or security). Then you have to worry about licensing and updates. Using Windows.Forms.WebBrowserwill work too, unless you're multi-threading. I'm sure you could work it out, but it felt too complicated for my needs.
你可以试试这个HTMLAgility包,但那是第三方。我觉得最好尝试使用内置的东西(除非第三方在功能和/或安全性方面非常优越)。然后您必须担心许可和更新。使用Windows.Forms.WebBrowser也可以,除非你是多线程的。我相信你可以解决这个问题,但感觉太复杂了,无法满足我的需求。
回答by Mike Feltman
Try the HTMLAgility Pack.
试试 HTMLAgility 包。
You can read about it here: http://html-agility-pack.net/
你可以在这里阅读它:http: //html-agility-pack.net/
The latest version is available via NuGet.
最新版本可通过 NuGet 获得。
So you would do something like:
所以你会做这样的事情:
Dim aHTML As New HTMLDocument
aHTML.Load(some string variable)
Note that you cannot load a URL in this fashion. I'm not sure if you really want to load a URL or if the URL provided was just for reference.
请注意,您不能以这种方式加载 URL。我不确定您是否真的想要加载一个 URL,或者所提供的 URL 是否仅供参考。

