2

I've used html agility pack before, and have had good results with a little bit of trial and error. I'm currently trying to use it to return a node set with an xpath I get by right- clicking "Copy XPath" in Firefox. I've done some searching, and I see that the browser will often add "tbody" for table tags. I tried it with removing this with no luck. Here is the xpath given to me by Firefox:

/html/body/p[3]/table/tbody/tr/td/table/tbody/tr[3]

Using it as- is throws the error: "Value cannot be null. Parameter name: source."

This occurs on line:

nodeList = htmlDoc.DocumentNode.SelectNodes("/html/body/p[3]/table/tbody/tr/td/table/tbody/tr[3]").ToList();

I'll continue to read, in the meantime if this is an easy fix to anyone, I'd appreciate a tip.

Update: This is the actual code:

protected override List<IDataPoint> ReturnDataPointsFromIndividualAddressString(string AddressString)
{
       List<IDataPoint> earningsAnnouncements = new List<IDataPoint>(); //Not used, yet..

       HtmlWeb hwObject = new HtmlWeb();
       HtmlDocument htmlDoc = hwObject.Load(AddressString);

       if (htmlDoc.DocumentNode != null)
       {
               List<HtmlNode> nodeList = new List<HtmlNode>();
               nodeList = htmlDoc.DocumentNode.SelectNodes("/html/body/p[3]/table/tbody/tr/td/table/tbody/tr[3]").ToList();
       }
}
StatsViaCsh
  • 2,600
  • 10
  • 44
  • 63

1 Answers1

7

It seems this error occurs on this line:

nodeList = htmlDoc.DocumentNode.SelectNodes("/html/body/p[3]/table/tbody/tr/td/table/tbody/tr[3]").ToList();

The thing is, if SelectNodes method doesn't find nodes by xpath expression passed it returns null. You could find more information in this answer to a similar question HTML Agility Pack Null Reference. And then you call a ToList() method on a null object which is actually causes an NullReferenceException.

To avoid this check the this variable against null like this:

var nodes = htmlDoc.DocumentNode.SelectNodes(...);
if (nodes != null)
{
     nodeList = nodes.ToList();
}
Community
  • 1
  • 1
Oleks
  • 31,955
  • 11
  • 77
  • 132
  • Actually the error occurs on: HtmlDocument htmlDoc = hwObject.Load(AddressString); – StatsViaCsh Apr 03 '12 at 20:09
  • @StatsViaCs, it's strange. I can't see how`HtmlWeb.Load` throws an `NullReferenceException` with the `source` parameter. Could you post the value of `AddressString` which caused this error? – Oleks Apr 03 '12 at 20:20
  • My mistake Alex.. it does indeed throw it on the line you suggested, I had stepped away for a bit, sorry. – StatsViaCsh Apr 03 '12 at 20:25
  • You're right, that is indeed null, so I'll use your method to avoid the exception in the future. I guess my question is what is wrong with my xpath? – StatsViaCsh Apr 03 '12 at 20:30
  • 1
    @StatsViaCsh, it depends on the HTML source, and on what nodes are you trying to find – Oleks Apr 03 '12 at 20:33