© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
41 replies
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);
    }
}
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
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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Scrape with HtmlAgilityPack
C#CC# / help
3y ago
❔ Getting Info From HTML File
C#CC# / help
4y ago
Problem with Parsing contents from .XML file when file is being read
C#CC# / help
3y ago
Parsing an arbitrary file path as a Uri or similar
C#CC# / help
3y ago