I’m using the HTML agility pack to scrape a website. I’m looking though all the div tags that contain “a” elements for certain text. Here is the sample code:
var showLocations = (from div in document.DocumentNode.Descendants("div")
from a in div.DescendantNodes().Where(x => x.Name == "a")
where a.Attributes["href"].Value.Contains("show_locs=Y")
select a).SingleOrDefault();
However, instead of returning null if the element cannot be found, it throws a “Null Reference Exception”. Why is that?
My guess is that there are
<a>tags withouthrefattributes (i.e anchors instead of links). trya.GetAttributeValue("href", "").Contains...instead.