I am using HTML Agility pack in Xamarin and I keep getting the following error. System.NullReferenceException: Object reference not set to an instance of an object.
The error is coming from the foreach loop and I saw that the nodes
variable was null
, but I don't know why. It is probably because my node selection is not right, but I don't how to fix it as I already tried different selections.
string source = getSource(url);
if (source == "") return;
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(source);
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@id='events']");
var nodeText = "";
foreach (HtmlNode node in nodes)
{
nodeText += node.InnerText;
}
lbl_geheel.Text = nodeText;
So I guess ("//div[@id='events']")
is not right.
This is the HTML code that I am scraping:
<div id="events" class="">
From the following page: https://www.espn.in/football/scoreboard