0

I have a function where I am trying to get some text from this webpage:

http://www.nla.gd/winning-numbers/

public static string get_webpage(string url)
{
    HtmlWeb web = new HtmlWeb();
    HtmlDocument doc = web.Load(url);
    string date = doc.DocumentNode.InnerText;
    string lotto_winning_numbers = doc.DocumentNode.SelectNodes("//[@id=\"main\"]/div/strong/div/div[2]/div[1]/div[1]").ToString();

    return lotto_winning_numbers;
}

When I run the function I get a NULL Exception.

Is my xpath correct?

SBI
  • 2,322
  • 1
  • 17
  • 17
Dami1
  • 23
  • 3

1 Answers1

1

You can't have filter by itself in XPath (like [@id='main']). You need to apply filter to collection of nodes like div or *.

Note that you also want to combine values elements in resulting collection, not convert collection itself to string.

Something like:

// Note "*" in front of filter
var lotto_winning_numbers = doc.DocumentNode.SelectNodes(
       "//*[@id=\"main\"]/div/strong/div/div[2]/div[1]/div[1]");

// lotto_winning_numbers is collection of nodes here.
return lotto_winning_numbers == null ? String.Empty : 
       String.Join(", ", lotto_winning_numbers);

Check MSDN article XPath Examples or many other tutorials to learn more.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179