DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Scrolling With Konva.js and React
  • Reading an HTML File, Parsing It and Converting It to a PDF File With the Pdfbox Library
  • React, Angular, and Vue.js: What’s the Technical Difference?
  • Micro Frontends With Example

Trending

  • When RabbitMQ Accepts a Message but Does Not Route It to a Queue
  • Fix the Target, Precompute Once: A Backend-Free Word-Ladder Solver With a BFS Distance Field
  • Why Vulnerability Management Fails Without Reliable Asset Data
  • Rebuilding Elasticsearch Under Load: Unsafe Watermarks and a Sampling Blind Spot

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Parsing HTML: Selecting the Right Library (Part 2)

Parsing HTML: Selecting the Right Library (Part 2)

With Java out of the way, now we compare two C# libraries designed for parsing HTML, considering their strengths and weaknesses.

By 
Gabriele Tomassetti
·
Updated Sep. 25, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
10.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Last time, we looked over the various HTML parsers you can consider when working with Java. This time, we'll examine a couple of popular C# libraries worth considering as we examine their features, benefits, and drawbacks when processing HTML.

C#

AngleSharp

The ultimate angle brackets parser library parsing HTML5, MathML, SVG and CSS to construct a DOM based on the official W3C specifications.

AngleSharp is, quite simply, the default choice for whenever you need a modern HTML parser for a C# project. In fact, it does not just parse HTML5, but also its most-used companions: CSS and SVG. There is also an extension to integrate scripting in the context of parsing HTML documents: both C# and JavaScript, based on Jint. That means that you can parse HTML documents after they have been modified by JavaScript — both the JavaScript included in the page or a script you add yourself.

AngleSharp fully supports modern conventions for easy manipulation, like CSS selectors and jQuery-like constructs. But it is also well-integrated with the .NET world, with support for LINQ for DOM elements. The author mentions that it may evolve into something more than a parser, but for the moment, it can do simple things like submitting forms.

The following example from the documentation shows a few features of AngleSharp.

var parser = new HtmlParser();
var document = parser.Parse("<ul><li>First item<li>Second item<li class='blue'>Third item!<li class='blue red'>Last item!</ul>");

//Do something with LINQ
var blueListItemsLinq = document.All.Where(m => m.LocalName == "li" && m.ClassList.Contains("blue"));

//Or directly with CSS selectors
var blueListItemsCssSelector = document.QuerySelectorAll("li.blue");

Console.WriteLine("Comparing both ways ...");

Console.WriteLine();
Console.WriteLine("LINQ:");

foreach (var item in blueListItemsLinq)
    Console.WriteLine(item.Text());

Console.WriteLine();
Console.WriteLine("CSS:");

foreach (var item in blueListItemsCssSelector)
    Console.WriteLine(item.Text());


The documentation may contain all the information you need, but it certainly could use better organization. For the most part, it is delivered within the GitHub project, but there are also tutorials on CodeProject by the author of the library.

HtmlAgilityPack

HtmlAgilityPack was once considered the default choice for HTML parsing with C#, although some say that was due to the lack of better alternatives — because the quality of the code was low. In any case, it was essentially abandoned for the last few years, until it was recently revived by ZZZ Projects.

In terms of features and quality, it is quite lacking, at least compared to AngleSharp. Support for CSS selector, necessary for modern HTML parsing, and support for .NET Standard, necessary for modern C# projects, are on the roadmap. On the same document, there is also a planned cleanup of the code.

If you are in need of things like XPath, HtmlAgilityPack should be your best choice. In other cases, I do not think it is the best choice right now — unless you are already using it. That is especially true since there is no documentation. That being said, the new maintainer and the prospect for better features are a good reason to keep using it if you are already a user.

// Load an HTML document
var url = "http://html-agility-pack.net/";
var web = new HtmlWeb();
var doc = web.Load(url);

// Get value with XPath
var value = doc.DocumentNode
       .SelectNodes("//td/input")
               .First()
       .Attributes["value"].Value;


Conclusion

Now that you've seen the heavy hitters in the C# world of parsing HTML, next time, we'll take a look at the options out there for Python. 

HTML Library

Published at DZone with permission of Gabriele Tomassetti. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Scrolling With Konva.js and React
  • Reading an HTML File, Parsing It and Converting It to a PDF File With the Pdfbox Library
  • React, Angular, and Vue.js: What’s the Technical Difference?
  • Micro Frontends With Example

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook