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
  • c#
  • html-agility-pack

HtmlAgilityPack - Remove child nodes but retain inner text for the main node

Dec 29, 2012 See the question and my original answer on StackOverflow

This code:

    HtmlDocument doc = new HtmlDocument();
    doc.Load(MyTextHtml);

    HtmlNode node = doc.DocumentNode.SelectSingleNode("//p1/following-sibling::text()");
    Console.WriteLine(node.InnerText.Trim());

will output this:

"script text"

Here is link on XPATH axes that should get you started.

  • SmoBlog © 2010-2026 Simon Mourier. All rights reserved.
  • Privacy & Terms