views:

152

answers:

3

HtmlAgilityPack kind of sucks IMO. They made some really wonky design decisions.

  • When using SelectNodes, if no nodes are found, it returns null rather than an empty set, so you can't just foreach over it without a null check.
  • When trying to select children with node.SelectNodes it actually searches from the document root (which I thought was discarded -- why is the node carrying more info than it should?) unless you use descendant:: which is not obvious nor expected behavior at all, IMO.
  • HtmlDocument.Load doesn't return the root node, which is what you'd want 99% of the time, I think

You might disagree with that of course, but that's not the point. I'm looking for something different. Something that behaves a little more sane, or something that uses jQuery syntax would be even better. Suggestions?

+1  A: 

For the examples you've given, it should be fairly easy to alter the behaviour to that which you desire. Since HtmlAgilityPack is open source, have you considered taking a local fork and making those changes?

AdamRalph
For the time being I've just wrapped it with my own functions, but still. If there's something else out there a little more aligned with my philosophies, I'm not going to waste my efforts :) I only dabble in HTML parsing once in awhile for small projects, so I don't think it's worth my time to overhaul it to be the way I think it ought to be.
Mark
+1  A: 

If you're just parsing the html, another option might be SgmlReader. If you're modifying the html, not so much. Don't recall how it behaves with respect to the issues you raised,but it worth checking out.

aciemian
As far as I can see, that library only converts malformed HTML into valid HTML... it says nothing about xpath/querying/traversing the DOM tree. I don't need to modify the document, but I *do* need to query it.
Mark
It turns it into valid xml in the form of an XmlDocument. Then you can call one of the XmlDocument.CreateNavigator() overloads to get an XPathNavigator object to perform xpath queries.
aciemian
+1  A: 

Started project called SharpQuery

Currently supports ID, class, tag, and attribute selectors.

a
a[href]
a[href^=http://stackoverflow.com]
.class
#id
Mark
I voted for SharpQuery + HTMLAgilityPack to merge a long time ago. Cos the HTML parser and DOM structure should be distinctly different from the query engine... Also, HTMLAgilityPack supports multiple query methods - XPath, LINQ and DOm traversal. SharpQuery on top of that would be awesome
CVertex
Oh, wait u just started that project? I proposed joining a different project that does the exact same thing as SharpQuery... lemme find it
CVertex
FOund it - http://code.google.com/p/fizzler/ and here's my merge request http://htmlagilitypack.codeplex.com/Thread/View.aspx?ThreadId=76383
CVertex
@CVertex: Could have mentioned Fizzler before I started this project :p I just added a neat regex selector `a[href %= /caseInsensitive/i]`
Mark
Anyway, SharpQuery is just a set of extension methods that work on `IEnumerable<HtmlNode>`, so it should play pretty nicely with HtmlAgilityPack. Might make it based on `IXPathNavigable` instead if I can figure out how... then it should work with any XML document.
Mark