C#C
C#2y ago
FatTony

✅ Parsing a Link from an HTML file with HTMLAgilityPack

Hi, I'm having a bit of trouble parsing an HTML file to extract a link.
I'm using HTMLAgilityPack to do this as it seemed simple enough for what I wanted.

In the latest variable I use SelectNodes and provide the XPATH to the link that I found using inspect element.
However, the selection returns null and the Console returns an error when writing.
Any tips?

using System;
using HtmlAgilityPack;
class Program
{

    static void Main(string[] args)
    {
        // Use HAP to fetch html from web.
        var link = "https://www.abs.gov.au/statistics/labour/employment-and-unemployment/labour-force-australia/dec-2023";
        HtmlWeb web = new HtmlWeb();
        var htmlDoc = web.Load(link);
        var latest = htmlDoc.DocumentNode.SelectNodes("//*[@id=\"block-views-block-topic-releases-listing-topic-latest-release-block\"]/div/div/div/div/a").ToString();
        Console.WriteLine(latest);
    }
}

Console Error
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at Program.Main(String[] args) in /home/antonio/interview_macrobond/Program.cs:line 12
Was this page helpful?