I'm getting started with Html Agility Pack but I'm struggling with it throwing a NullReferenceException
for items I know for a fact exists.
For example, take this code:
doc.DocumentNode.Descendants("div").ToList()[2]
This returns a valid HtmlNode
on which I can do node.Attributes["id"]
and get the value of the id
attribute ("somevalue" in this case).
However, if I try to get this specific element based on the attribute value I get a NullReferenceException
:
doc.DocumentNode.Descendants("div").FirstOrDefault(e => e.Attributes["id"].Value == "somevalue")
Why does this code throw a NullReferenceException
when an element with this attribute and this value actually exist?