Click here to Skip to main content
15,898,745 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
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 link.HasAttributes("title") and get another error

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

thanks
Posted

1 solution

Your code is making a huge assumption. The error occurs because you're assuming that root.SelectNodes("//a") is actually returning a collection of Nodes. You might want to check this return for null before you try and use it.

The HasAttributes property doesn't take any parameters, so I have no idea what you're trying to do by passing in one. You might want to read up on the documentation for HtmlNode.HasAttibutes and understand what it does before you try and use it for something.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Jun-11 18:22pm    
Good points, a 5.
--SA
Cool Smith 10-Jun-11 4:03am    
how do i check then if an '<'a'>' link has a particular attribute

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900