2

I'm following the answer given here: https://stackoverflow.com/a/18066671

string Url = "http://www.owgr.com/ranking";
        HtmlWeb web = new HtmlWeb();
        HtmlDocument doc = web.Load(Url);
        string worldRanking = doc.DocumentNode.SelectNodes("//*[@id=\"ranking_table\"]/div[2]/table/tbody/tr[1]/td[1]")[0].InnerText;

But the Xpath is returning a null reference. I've tried several variations but nothing's working, I've never used XPath before, am I missing something?

Community
  • 1
  • 1
Jimmy
  • 2,191
  • 6
  • 25
  • 45
  • are you sure, your doc contains table and div?? – Saghir A. Khatri Mar 18 '14 at 19:24
  • here is the result that i got by running `ranking_table` only. "RAnkings per page \n All \n 25 \n 50 \n 100 \n 150" – Saghir A. Khatri Mar 18 '14 at 19:26
  • possible duplicate of [Why does my XPath query (scraping HTML tables) only work in Firebug, but not the application I'm developing?](http://stackoverflow.com/questions/18241029/why-does-my-xpath-query-scraping-html-tables-only-work-in-firebug-but-not-the) – Jens Erat Mar 18 '14 at 20:49

1 Answers1

2

There is no <tbody/> element in that table, remove the /tbody axis step and your query works totally fine. See "Why does my XPath query (scraping HTML tables) only work in Firebug, but not the application I'm developing?" for details.

Additional hint: XPath also supports single quotes for strings, so you don't need to escape the double quotes.

This XPath expression will return the element you're looking for:

//*[@id='ranking_table']/div[2]/table/tr[1]/td[1]
Community
  • 1
  • 1
Jens Erat
  • 37,523
  • 16
  • 80
  • 96