开发者

HtmlAgilityPack interfering with my code (not a HtmlAgilityPack question)

开发者 https://www.devze.com 2023-01-16 01:57 出处:网络
Here is a snip of my code: Dim content As String = \"\" Dim web As New HtmlAgilityPack.HtmlWeb Dim doc As New HtmlAgilityPack.HtmlDocument()

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.Doc开发者_如何学PythonumentStream)
    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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消