首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HtmlAgilityPack干扰我的代码(不是HtmlAgilityPack问题)

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

Stack Overflow用户
提问于 2010-09-13 00:39:38
回答 1查看 1.3K关注 0票数 0

下面是我的代码片段:

代码语言:javascript
复制
 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时,代码可以工作,但是我需要导入它来做一些事情,现在它会干扰这一点。请帮帮忙。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-09-13 01:02:24

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

您可能只需修复这一行:

代码语言:javascript
复制
' Here the VB compiler must think you mean HtmlAgilityPack.HtmlDocument: '
Dim htmlDoc As HtmlDocument

...by将其更改为:

代码语言:javascript
复制
Dim htmlDoc As System.Windows.Forms.HtmlDocument

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

代码语言:javascript
复制
Imports AgilityDocument = HtmlAgilityPack.HtmlDocument
Imports FormsDocument = System.Windows.Forms.HtmlDocument

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

代码语言:javascript
复制
Dim doc As New AgilityDocument
Dim htmlDoc As FormsDocument = Me.WebBrowser2.Document
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3697291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档