See the question and my original answer on StackOverflow

The Html Agility Pack has a "design bug" that returns a null for an empty collection. So you need to do this instead:

HtmlNodeList list = htmldoc.DocumentNode.SelectNodes("//a[@HREF]");
if (list != null)
  foreach (HtmlNode link in list)

And by the way, all tags that are specified in the XPATH expression must be lowercase, even if they are declared differently in the HTML text (because HTML is case insensitive, the default Html Agility Pack XPATH convention is to use lowercase tags). So you should write this instead:

HtmlNodeList list = htmldoc.DocumentNode.SelectNodes("//a[@href]");