HtmlAgilityPack 干扰我的代码(不是 HtmlAgilityPack 问题)
这是我的代码片段:
Dim content As String = ""
Dim web As New HtmlAgilityPack.HtmlWeb
Dim doc As New HtmlAgilityPack.HtmlDocument()
doc.Load(WebBrowser1.DocumentStream)
Dim hnc As HtmlAgilityPack.HtmlNodeCollection = doc.DocumentNode.SelectNodes("//div[@class='address']/preceding-sibling::h3[@class='listingTitleLine']")
For Each link As HtmlAgilityPack.HtmlNode In hnc
Dim replaceUnwanted As String = ""
replaceUnwanted = link.InnerText.Replace("&", "&") '
<span style="white-space:pre"> </span> content &= replaceUnwanted & vbNewLine
Next
'I have a bunch of code here I removed ------------------------------
Dim htmlDoc As HtmlDocument = Me.WebBrowser2.Document
Dim visibleHtmlElements As HtmlElementCollection = htmlDoc.GetElementsByTagName("TD")
Dim found As Boolean = False
For Each str As HtmlElement In visibleHtmlElements
If Not String.IsNullOrEmpty(str.InnerText) Then
Dim text As String = str.InnerText
If str.InnerText.Contains(parts(2)) Then
found = True
End If
End If
Next
我收到 Me.WebBrowser2.Document 错误:
““System.Windows.Forms.HtmlDocument”类型的值无法转换为“HtmlAgilityPack.HtmlDocument” '.
另一个 htmlDoc.GetElementsByTagName:
'GetElementsByTagName' 不是 'HtmlAgilityPack.HtmlDocument' 的成员。
当我不使用 HAP 时,该代码可以正常工作,但我需要导入它来做某事,现在它干扰了这个,请帮忙。
Here is a snip of my code:
Dim content As String = ""
Dim web As New HtmlAgilityPack.HtmlWeb
Dim doc As New HtmlAgilityPack.HtmlDocument()
doc.Load(WebBrowser1.DocumentStream)
Dim hnc As HtmlAgilityPack.HtmlNodeCollection = doc.DocumentNode.SelectNodes("//div[@class='address']/preceding-sibling::h3[@class='listingTitleLine']")
For Each link As HtmlAgilityPack.HtmlNode In hnc
Dim replaceUnwanted As String = ""
replaceUnwanted = link.InnerText.Replace("&", "&") '
<span style="white-space:pre"> </span> content &= replaceUnwanted & vbNewLine
Next
'I have a bunch of code here I removed ------------------------------
Dim htmlDoc As HtmlDocument = Me.WebBrowser2.Document
Dim visibleHtmlElements As HtmlElementCollection = htmlDoc.GetElementsByTagName("TD")
Dim found As Boolean = False
For Each str As HtmlElement In visibleHtmlElements
If Not String.IsNullOrEmpty(str.InnerText) Then
Dim text As String = str.InnerText
If str.InnerText.Contains(parts(2)) Then
found = True
End If
End If
Next
Im getting an error for Me.WebBrowser2.Document:
"Value of type 'System.Windows.Forms.HtmlDocument' cannot be converted to 'HtmlAgilityPack.HtmlDocument'.
And another one for htmlDoc.GetElementsByTagName:
'GetElementsByTagName' is not a member of 'HtmlAgilityPack.HtmlDocument'.
The code worked when I wasnt using HAP, but I needed to import it to do something and now its interfering with this. Help please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是
HtmlAgilityPack
和System.Windows.Forms
都有一个名为HtmlDocument
的类型。您可能只需修复这一行:
...将其更改为:
一般解决此类问题的一个好方法是使用
Imports
语句为具有冲突名称的类型提供别名,如下所示:然后您将在代码中使用这些别名之一,而不是键入共享名称。因此,例如:
The problem is that both
HtmlAgilityPack
andSystem.Windows.Forms
have a type calledHtmlDocument
.You could probably just fix this one line:
...by changing it to this:
In general a good way to resolve this kind of issue is by using the
Imports
statement to provide aliases for the types with conflicting names, like so:Then you would use one of these aliases in your code instead of typing out the shared name. So, for example: