2010-09-13 54 views
0

這裏是我的代碼剪斷干擾: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時,代碼正常工作,但我需要導入它來執行某些操作,現在它干擾了此操作。請幫助。

回答

3

問題是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 
+0

謝謝Dan幫助了很多,非常感謝。 – Datadayne 2010-09-13 01:13:45