Parsing HTML in C# (.NET): From Simple to Complex
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 efficiently extract data from HTML documents, even if the markup is invalid. In this article, we will look at three of the most popular tools: HtmlAgilityPack, AngleSharp, and CsQuery, and also analyze practical examples of their use.
1. HtmlAgilityPack — The Classic Choice
HtmlAgilityPack (HAP) is the most common library for parsing HTML in C#. It supports XPath and LINQ, making it ideal for working with hierarchical structures. HAP automatically corrects invalid HTML markup, which is especially useful when parsing "raw" pages.
Installation via NuGet:
Install-Package HtmlAgilityPackExample: Extracting all links from a page
using HtmlAgilityPack;
string url = "https://example.com";var web = new HtmlWeb();var doc = web.Load(url);
var links = doc.DocumentNode.SelectNodes("//a[@href]");if (links != null){ foreach (var link in links) { string href = link.GetAttributeValue("href", ""); string text = link.InnerText.Trim(); Console.WriteLine($"{text}: {href}"); }}Example: Extracting data from a table
var tableRows = doc.DocumentNode.SelectNodes("//table[@id='data-table']//tr");foreach (var row in tableRows){ var cells = row.SelectNodes("td"); if (cells != null) { var rowData = cells.Select(c => c.InnerText.Trim()).ToList(); Console.WriteLine(string.Join(" | ", rowData)); }}2. AngleSharp — A Modern Alternative
AngleSharp is a more modern library, entirely written in C# and compliant with W3C specifications. It supports CSS selectors, making it convenient for those accustomed to jQuery-like syntax. AngleSharp can also work with DOM, SVG, and MathML.
Installation via NuGet:
Install-Package AngleSharpExample: Parsing using CSS selectors
using AngleSharp;using AngleSharp.Dom;
var config = Configuration.Default.WithDefaultLoader();var context = BrowsingContext.New(config);var document = await context.OpenAsync("https://example.com");
// Get all elements with the class "product"var products = document.QuerySelectorAll(".product");foreach (var product in products){ var name = product.QuerySelector(".product-name")?.TextContent.Trim(); var price = product.QuerySelector(".product-price")?.TextContent.Trim(); Console.WriteLine($"{name}: {price}");}Example: Extracting JSON from a script
var scripts = document.QuerySelectorAll("script[type='application/ld+json']");foreach (var script in scripts){ var json = script.TextContent; // Next, you can deserialize the JSON using System.Text.Json Console.WriteLine(json);}3. CsQuery — jQuery-like Syntax
CsQuery is a port of jQuery for C# that provides a familiar API for working with the DOM. It is especially convenient for those migrating from JavaScript to C#. However, it is worth noting that the project is updated less frequently than AngleSharp and may not support the latest versions of .NET.
Installation via NuGet:
Install-Package CsQueryExample: Extracting text from elements
using CsQuery;
string html = "<div class='content'><p>Hello, world!</p></div>";var dom = CQ.Create(html);
var text = dom[".content Also in library
Working with Dates in C++: Overview of Libraries for Time Manipulation
Working with Images in Kotlin: Libraries, Tools, and Examples
JsonWebToken for JavaScript (Node.js): Complete Guide with Examples
Working with S3 and Cloud Storage in JavaScript (Node.js) — @aws-sdk/client-s3 Library
Caching in Node.js: Redis, Memcached, and Node-Cache