HtmlAgilityPack 干扰我的代码(不是 HtmlAgilityPack 问题)

发布于 2024-09-19 01:41:57 字数 1517 浏览 3 评论 0原文

这是我的代码片段:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城月光淡如水﹏ 2024-09-26 01:41:57

问题是 HtmlAgilityPackSystem.Windows.Forms 都有一个名为 HtmlDocument 的类型。

您可能只需修复这一行:

' Here the VB compiler must think you mean HtmlAgilityPack.HtmlDocument: '
Dim htmlDoc As HtmlDocument

...将其更改为:

Dim htmlDoc As System.Windows.Forms.HtmlDocument

一般解决此类问题的一个好方法是使用 Imports 语句为具有冲突名称的类型提供别名,如下所示:

Imports AgilityDocument = HtmlAgilityPack.HtmlDocument
Imports FormsDocument = System.Windows.Forms.HtmlDocument

然后您将在代码中使用这些别名之一,而不是键入共享名称。因此,例如:

Dim doc As New AgilityDocument
Dim htmlDoc As FormsDocument = Me.WebBrowser2.Document

The problem is that both HtmlAgilityPack and System.Windows.Forms have a type called HtmlDocument.

You could probably just fix this one line:

' Here the VB compiler must think you mean HtmlAgilityPack.HtmlDocument: '
Dim htmlDoc As HtmlDocument

...by changing it to this:

Dim htmlDoc As System.Windows.Forms.HtmlDocument

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:

Imports AgilityDocument = HtmlAgilityPack.HtmlDocument
Imports FormsDocument = System.Windows.Forms.HtmlDocument

Then you would use one of these aliases in your code instead of typing out the shared name. So, for example:

Dim doc As New AgilityDocument
Dim htmlDoc As FormsDocument = Me.WebBrowser2.Document
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文