2011-06-06 42 views
1

htmlagilitypack我有這樣的代碼越來越<a>標籤和屬性與vb.net

Dim htmldoc As HtmlDocument = New HtmlDocument() 
htmldoc.LoadHtml(strPageContent) 
Dim root As HtmlNode = htmldoc.DocumentNode 

For Each link As HtmlNode In root.SelectNodes("//a") 
    If link.HasAttributes("href") Then doSomething() 'this doesn't work because hasAttributes only checks whether an element has attributes or not 
Next 

,但我得到一個錯誤Object reference not set to an instance of an object.

文檔包含至少一個錨標記?我如何檢查屬性是否退出?

我想這if link.HasAttributes("title") then,並得到其他錯誤

Public ReadOnly Property HasAttributes() As Boolean' has no parameters and its return type cannot be indexed.

回答

1

如果HtmlAgilityPack支持此XPATH選擇,你可以用//a[@href]

For Each link as HtmlNode In root.SelectNodes("//a[@href]") 
    doSomething() 
Next 

更換//a否則,您可以使用Attributes屬性:

For Each link as HtmlNode In root.SelectNodes("//a") 
    If link.Attributes.Any(Function(a) a.Name = "href") Then doSomething() 
Next 
+0

@jay不是隻提取鏈接,但標題attrib和innerText,所以我怎麼做到這一點? – Smith 2011-06-06 16:48:46

+0

@Smith'HasAttributes'不是一個方法,它是一個屬性 - 你不能使用括號和參數。還有一個'Attributes'屬性,它將返回與給定元素節點關聯的所有屬性。哪行代碼導致了'NullReferenceException'? – Jay 2011-06-06 17:36:37

+1

@史密斯更多到你的問題,看我的第二個例子。在'For ... Each'塊中,你可以檢查'link.Attributes'來看它是否包含'href'屬性,但你也可以檢查其他屬性和/或使用'link.InnerText'屬性來獲取錨標籤的內部文本。 – Jay 2011-06-06 17:40:30

0
Dim htmldoc As HtmlDocument = New HtmlDocument() 
htmldoc.LoadHtml(strPageContent) 
Dim root As HtmlNode = htmldoc.DocumentNode 

var nodes = root.SelectNodes("//a[@href and @title]") 
if (nodes <> Null) Then 
    For Each link As HtmlNode In nodes 
     If link.HasAttributes("href") Then doSomething() 'this doesn't work because hasAttributes only checks whether an element has attributes or not 
    Next 
end if 

此外,您可以檢查屬性: link.Attributes [「title」]如果爲null,則不具有屬性。 相同鏈接。屬性[「href」]等。

屬性link.HasAttributes只顯示該標籤具有任何屬性,它是bool值。