Skip to content Skip to sidebar Skip to footer

C# Html Agility Pack Single Select Node Returning Null

I have a web scraper developed using C#, windows forms and the HTML Agility Pack. I had it all working great when the site changed it's code and broke it. I know it happens often

Solution 1:

First you should check whether

 doc.DocumentNode.SelectSingleNode("//h1[@class='producttitle']")

returns null.

If it is null you'll get the the NullReferenceException from null.InnerText

Solution 2:

try below

var varitem = doc.DocumentNode.SelectSingleNode("//h1[@class='producttitle']");

SelectSingleNode can return null and also you better check whether InnerText also not null or empty as well

if (varitem == null || string.IsNullOrEmpty(varitem.InnerText))
              MessageBox.Show("no titles");

Post a Comment for "C# Html Agility Pack Single Select Node Returning Null"