- An HTML parsing library
- A class library that parses HTML with XPath
- It’s recommended to get a basic understanding of XPath first
- XPath retrieves node values and attribute values within XML using path expressions
- It is an open‑source .NET library
- Legacy source‑code repository: http://htmlagilitypack.codeplex.com/
Official source code is now hosted on GitHub: https://github.com/zzzprojects/html-agility-pack
Html Agility Pack is one of the most classic open‑source HTML parsers in the .NET ecosystem. Written in C#, it is widely used for web scraping, HTML content extraction and HTML document modification.
Its biggest advantage: it handles malformed and incomplete HTML (most real‑world web pages are not well‑formed closed XML. Standard XmlDocument cannot parse them, while HAP provides automatic fault tolerance).
XPath was originally designed for querying XML documents. After HAP loads HTML into a DOM tree, you can also query nodes using XPath syntax:
- Filter tags, fetch text and extract attributes via path expressions
- Common methods:
SelectSingleNode("xpath expression"): Matches the first node foundSelectNodes("xpath expression"): Matches all matching nodes
You no longer need to download source code manually; just install the package from NuGet
Install-Package HtmlAgilityPack
Supports major .NET platforms: .NET Framework, .NET Core, .NET 5‑.NET9, MAUI and more.
Here is a minimal example. Create a console application inside Visual Studio
Then add the following snippet inside your Main method
using HtmlAgilityPack;
//1.Load HTML text
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml("HTML string of web page");
//2.Query elements with XPath
HtmlNode titleNode = htmlDoc.DocumentNode.SelectSingleNode("//title");
string title = titleNode?.InnerText;Code language: C++ (cpp)
You can then parse the target HTML string.
Introduction