HTML Parsing in C# (.NET): Overview of HtmlAgilityPack and AngleSharp Libraries
HTML parsing is one of the key tasks when developing web scrapers, content analyzers, and automation tools. In the C# (.NET) ecosystem, there are several mature libraries that allow you to extract data from HTML documents, manipulate the DOM tree, and perform queries on elements. In this article, we will take a detailed look at the two most popular libraries: HtmlAgilityPack and AngleSharp, their capabilities, syntax, and usage examples.
1. HtmlAgilityPack — A Time-Tested Tool
HtmlAgilityPack (HAP) is one of the oldest and most widely used libraries for HTML parsing in .NET. It supports both .NET Framework and .NET Core / .NET 5+. Its main advantage is simplicity and fault tolerance when working with "dirty" HTML (invalid markup, unclosed tags).
Installation
// Via NuGet Package ManagerInstall-Package HtmlAgilityPack
// Or via .NET CLIdotnet add package HtmlAgilityPackBasic Example: Extracting H1 Headings
using HtmlAgilityPack;
string html = @"<html><body> <h1>Heading 1</h1> <h1>Heading 2</h1> <p>Paragraph text</p></body></html>";
var doc = new HtmlDocument();doc.LoadHtml(html);
var h1Nodes = doc.DocumentNode.SelectNodes("//h1");if (h1Nodes != null){ foreach (var node in h1Nodes) { Console.WriteLine(node.InnerText.Trim()); }}// Output:// Heading 1// Heading 2Using XPath and CSS Selectors
HtmlAgilityPack uses XPath for navigation by default. However, with the HtmlAgilityPack.CssSelectors package, you can use CSS selectors:
// Installation: dotnet add package HtmlAgilityPack.CssSelectorsusing HtmlAgilityPack.CssSelectors;
// ... after loading the documentvar links = doc.DocumentNode.QuerySelectorAll("a.external-link");foreach (var link in links){ Console.WriteLine(link.GetAttributeValue("href", ""));}Parsing a Real Website
using System.Net.Http;using HtmlAgilityPack;
var url = "https://example.com/articles";var httpClient = new HttpClient();var html = await httpClient.GetStringAsync(url);
var doc = new HtmlDocument();doc.LoadHtml(html);
// Extract all article headingsvar articles = doc.DocumentNode.SelectNodes("//article/h2/a");if (articles != null){ foreach (var article in articles) { var title = article.InnerText.Trim(); var link = article.GetAttributeValue("href", ""); Console.WriteLine($"{title} -> {link}"); }}2. AngleSharp — A Modern Alternative
AngleSharp is a newer library that offers full support for HTML5, CSS3, JavaScript (limited), and DOM specifications. It is ideal for tasks requiring precise compliance with browser rendering. It supports .NET Standard 2.0+, meaning compatibility with .NET Framework 4.6.1+ and .NET Core.
Installation
dotnet add package AngleSharpBasic Example: Extracting All Links
using AngleSharp;using AngleSharp.Dom;using AngleSharp.Html.Parser;
var html = @"<html><body> <a href='https://example.com'>Example</a> <a href='https://test.com'>Test</a></body></html>";
var parser = new HtmlParser();IDocument document = parser.ParseDocument(html);
var links = document.QuerySelectorAll("a");foreach (var link in links){ Console.WriteLine($"{link.TextContent} -> {link.GetAttribute("href")}");}// Output:// Example -> https://example.com// Test -> https://test.com