I am using HTMLAgility to parse HTML document. I want to get all the images tag within the html document. Here is my code.
In some case, I am getting "imgs.Count" equals to 4. But I get an error saying 'Error: Object reference not set to an instance of an object'.
var imgs = document.DocumentNode.SelectNodes("//img");
if (imgs != null)
{
Console.WriteLine("imgs_count" + imgs.Count);
foreach (var img in imgs) {
var w = img.Attributes["width"].Value;
var h = img.Attributes["height"].Value;
Console.WriteLine("w=" + w + " h=" + h);
}
}
When I look at my source, it does have 4 image tag. So why I am getting the error saying object reference not set?