getting <a> tags and attribute with htmlagilitypack with vb.net

半世苍凉 提交于 2019-12-02 12:18:11

问题


i have this code

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

but am getting an error Object reference not set to an instance of an object.

the document contains at least one anchor-tag? how do i check if an attribute exits?

i tried this if link.HasAttributes("title") then and get another error

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


回答1:


If HtmlAgilityPack supports this XPATH selector, you can replace //a with //a[@href]

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

Otherwise, you can use the Attributes property:

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



回答2:


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

Also, you can check for attributes: link.Attributes["title"] if null, then doesnt have attribute. Same link.Attributes["href"] and etc.

Property link.HasAttributes shows only that tag has any attribute, it is bool value.



来源:https://stackoverflow.com/questions/6252438/getting-a-tags-and-attribute-with-htmlagilitypack-with-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!