本文介绍了得到< a>带有vag.net的htmlagilitypack的标签和属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码
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.
该文档至少包含一个锚标签?如何检查属性是否退出?
the document contains at least one anchor-tag? how do i check if an attribute exits?
我尝试了这个if link.HasAttributes("title") then
并遇到了另一个错误
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.
推荐答案
如果HtmlAgilityPack支持此XPATH选择器,则可以将//a
替换为//a[@href]
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
否则,您可以使用Attributes
属性:
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
这篇关于得到< a>带有vag.net的htmlagilitypack的标签和属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!