AngleSharp for C# (.NET): Complete Guide with Examples
\n\nAngleSharp is a modern, high-performance library for parsing and manipulating HTML, CSS, XML, and SVG documents in the .NET ecosystem. It provides C# developers with a full implementation of the DOM (Document Object Model), closely aligned with W3C standards. Unlike many other parsers, AngleSharp does not just extract data from a string — it builds a complete DOM tree that can be manipulated just like in a browser: finding elements by selectors, modifying attributes, adding or removing nodes, and serializing the result back to HTML.
\n\nWhy is this library needed? In modern web development and automation, there is often a need to programmatically analyze HTML pages. This can include data parsing for analytics, web scraping (extracting content from websites), testing web applications, creating static site generators, or even writing your own browser engine. AngleSharp is ideal for these tasks because it is entirely written in C#, has no external dependencies (except .NET Standard), and works many times faster than, for example, using HtmlAgilityPack combined with regular expressions. Additionally, the library supports CSS selectors, making navigation through the DOM tree intuitive for any web developer.
\n\nAnother important advantage of AngleSharp is its modularity and extensibility. You can add additional packages for working with CSS, JavaScript engines (e.g., Jint), or for parsing SVG. The library is actively maintained by the community, has detailed documentation, and is used in production environments by many companies. In this guide, we will thoroughly cover installation, main methods and classes, and provide a real-world example of parsing a web page.
\n\nInstallation
\n\nAngleSharp is distributed via NuGet, the package manager for .NET. It can be installed in two ways:
\n\nVia .NET CLI (recommended)
\ndotnet add package AngleSharp\n\nVia Package Manager Console in Visual Studio
\nInstall-Package AngleSharp\n\nFor CSS parsing, additionally install the package:
\ndotnet add package AngleSharp.Css\n\nAfter installation, add the using directives to your code:
\nusing AngleSharp;\nusing AngleSharp.Dom;\nusing AngleSharp.Html.Parser;\n\nQuick Start — Minimal Working Example
\n\nLet's write a simple program that loads an HTML string and extracts the title (the <h1> tag) from it:
\n\nusing AngleSharp;\nusing AngleSharp.Html.Parser;\n\n// 1. Create the parser\nvar parser = new HtmlParser();\n\n// 2. Parse the HTML string into a DOM document\nvar html = @"<!DOCTYPE html>\n<html>\n<head><title>Test Page</title></head>\n<body>\n <h1>Hello, AngleSharp!</h1>\n <p>This is a paragraph of text.</p>\n</body>\n</html>";\n\nvar document = parser.ParseDocument(html);\n\n// 3. Find the h1 element and output its text\nvar h1 = document.QuerySelector("h1");\nConsole.WriteLine(h1?.TextContent); // Outputs: Hello, AngleSharp!\n\n// 4. Output the entire HTML back (serialization)\nConsole.WriteLine(document.DocumentElement.OuterHtml);\n\nThis example demonstrates the basic workflow with the library: parsing → finding an element → extracting data. Note that the QuerySelector method accepts a CSS selector, which is very convenient.
Main Methods, Classes, and Interfaces
\n\nAngleSharp provides a rich API. Below are 8 key elements you will need in 90% of cases.
\n\n1. HtmlParser — Main Class for Parsing
\nSignature:
Also in library
Recommendations