Simon Mourier's Avatar
Simon Mourier's blog (1807 answers on StackOverflow) about Microsoft technologies C#, C/C++, .NET, WinRT, WinUI3, AOT, P/Invoke, COM, Interop, DirectX, Direct2D, Windows, ...
  • 🔎︎ Search
  • Categories
  • Archives
  • About Me
  • asp.net
  • c#
  • html-agility-pack
  • xpath

Use predicates in HtmlAgilityPack, Xpath

Feb 16, 2012 See the question and my original answer on StackOverflow

This kind of code:

    HtmlDocument doc = new HtmlDocument();
    doc.Load(myHtmlFile);
    foreach (HtmlNode node in doc.DocumentNode.SelectNodes(
        "//div[@class='list3' and position() > 2 and position() < 6]/a[@class='href1']"))
    {
        Console.WriteLine("node:" + node.InnerText);
    }

will give you this result:

node:A3
node:A4
node:A5
  • SmoBlog © 2010-2026 Simon Mourier. All rights reserved.
  • Privacy & Terms