A Web Developer’s Guide to Parsing HTML with C#
What methods should devs use to parse HTML? Don't say regex! Instead, we present two options for parsing HTML using C#.
Dec 2nd, 2023 5:00am by
Image via Unsplash
Hello<div class="Hello">Hello<b>Hello</b></div>
C# Packages for Parsing HTML
I’ll look at two C# packages whose API styles will be similar to parallel packages for other languages. Keeping the problem in code means you can be flexible with how you interpret restraints and error conditions. Imagine that you have asked your housing residents to contribute to a yearly maintenance report that you will eventually represent as a web page. This year, you have asked for each contributing resident to present their report as a very simple HTML snippet. The styling will all be done later. The residents all happen to be geeks, and they agree. To restrain the exuberant residents, you devise a number of simple rules:- All the paragraphs must appear within a <div> with a class called “YearReport”.
- The first paragraph must have a title attribute, from which we will extract the title’s text.
- There should no more than three paragraphs in total.
<!DOCTYPE html>
<html>
Here is my report:
<div class="YearReport">
<p title="Report on Drains">Bad year for drains</p>
<p>This is a paragraph about drains.</p>
<p>This is another paragraph about drains.</p>
</div>
</html>
HTML Agility Pack (HAP)
I’ll start with HTML Agility Pack (or HAP), as that appears to be the most popular and is available as a NuGet package within Visual Studio. Our first thing will be to find the YearlyReport div that all content must be within. This uses XPath, which is unsurprising but not something I want to examine. We just want to check, first, that there exists a <div> that has the attributes class=”YearlyReport”:var node = htmlDoc.DocumentNode
.SelectSingleNode("//div[@class="YearReport"]");
The mysterious string is XPath, whose job is to describe positions within an HTML/XML document. The one above says simply “find a <div> anywhere in this document, that has a class attribute with the value ‘YearReport’”.
Any well-documented technology will already be within the purview of generative AI, making the XPath extra step much less of a barrier. Asking ChatGPT to create the required XPath statement — if not the whole HTML parsing example — will certainly work.
Here is the code needed to test the constraints to a reasonable degree:
using HtmlAgilityPack;
var htmlDoc = new HtmlDocument();
htmlDoc.Load("//Users/eastmad/Projects/TheNewStack/HtmlParser/GoodReport.html");
var node = htmlDoc.DocumentNode .SelectSingleNode("//div[@class="YearReport"]");
if (node != null)
{
HtmlNodeCollection paragraphnodes = node.SelectNodes("p");
int numberofparas = 0; if (paragraphnodes != null)
{
string title = paragraphnodes.First().GetAttributeValue("title", "No title node found");
Console.WriteLine($"The report title is: {title}");
numberofparas = paragraphnodes.Count;
}
Console.WriteLine($"There are {numberofparas} paragraphs. The suggested number is between 1 and 3.");
}
else Console.WriteLine($"This report is invalid: it needs a correctly defined <div>");
The report title is: Report on Drains
There are 3 paragraphs. The suggested number is between 1 and 3.
AngleSharp: A Non-XPath Option
However, I am now keen to see if I can use a library that doesn’t need XPath. AngleSharp originally didn’t support XPath, but it has lots of NuGet package love now. It uses CSS selectors that for many will be a simpler choice. Using the same examples and restrictions, let’s rewrite the above code for that library:
using AngleSharp.Html.Parser;
var htmlParser = new HtmlParser();
Stream file = File.OpenRead("//Users/eastmad/Projects/TheNewStack/HtmlParser/GoodREport.html");
var htmlDoc = htmlParser.ParseDocument(file);
var node = htmlDoc.QuerySelector("div.YearReport");
if (node != null)
{
var paragraphnodes = node.QuerySelectorAll("p");
int numberofparas = 0; if (paragraphnodes != null)
{
string? title = paragraphnodes.First().GetAttribute("title");
Console.WriteLine($"The report title is: {title}");
numberofparas = paragraphnodes.Count();
}
Console.WriteLine($"There are {numberofparas} paragraphs. The suggested number is between 1 and 3.");
}
else Console.WriteLine($"This report is invalid: it needs a correctly defined <div>");
Conclusion
I hope this goes to show that HTML can be used in a pipeline, without too much fear that it will be difficult to handle without tricky code. The kicker is that I still needed either XPath or CSS selectors to quickly hone in on the right <div>, which only adds to the allure of AI tool help.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.