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
  • xpath

Remove child throws exception

Mar 3, 2013 See the question and my original answer on StackOverflow

Since you're getting all spans recursively under the current node (that's what // means in XPATH), you may get a span that is not a child of node, but a grand child.

In this case the call node.RemoveChild(span) will fail as you see. What you want to do instead is span.ParentNode.RemoveChild(span).

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