Category Archives: Web

Html Agility Pack: Get all heading tags

A project I’m currently working on involves parsing a lot of HTML using the Html agility pack. One problem I came across was finding all heading level tags between 1 and 6 and here’s my solution using Xpath:

string xpathQuery = "//*[starts-with(name(),'h') and string-length(name()) = 2 and number(substring(name(), 2)) <= 6]";
var tags = htmlDocument.DocumentNode.SelectNodes(xpathQuery);

If someone has a better or alternative solution please let me know in the comments.