Ich habe diesen 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
aber bekomme einen Fehler Object reference not set to an instance of an object.
Das Dokument enthält mindestens einen Anker-Tag? Wie überprüfe ich, ob ein Attribut existiert?
Ich habe dies versucht, if link.HasAttributes("title") then
und einen anderen Fehler erhalten
Public ReadOnly Property HasAttributes() As Boolean' has no parameters and its return type cannot be indexed.
Wenn HtmlAgilityPack diesen XPATH-Selektor unterstützt, können Sie //a
durch //a[@href]
ersetzen
For Each link as HtmlNode In root.SelectNodes("//a[@href]")
doSomething()
Next
Andernfalls können Sie die Eigenschaft Attributes
:
For Each link as HtmlNode In root.SelectNodes("//a")
If link.Attributes.Any(Function(a) a.Name = "href") Then doSomething()
Next
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
Außerdem können Sie nach Attributen suchen: link.Attributes ["title"] wenn null, dann hat es kein Attribut. Same link.Attributes ["href"] und usw.
Property link.HasAttributes zeigt nur, dass das Tag ein Attribut hat, es ist bool value.