In the HtmlNode, the RemoveChild method has this overload:
public HtmlNode RemoveChild(HtmlNode oldChild, bool keepGrandChildren);
So here is how you do it:
HtmlDocument doc = new HtmlDocument(); doc.Load("yourfile.htm"); foreach (HtmlNode font in doc.DocumentNode.SelectNodes("//font")) { font.ParentNode.RemoveChild(font, true); }
EDIT: It appears that the Replace w / keepGrandChildren parameter does not work as expected, so this is an alternative implementation:
public static HtmlNode RemoveChild(HtmlNode parent, HtmlNode oldChild, bool keepGrandChildren) { if (oldChild == null) throw new ArgumentNullException("oldChild"); if (oldChild.HasChildNodes && keepGrandChildren) { HtmlNode prev = oldChild.PreviousSibling; List<HtmlNode> nodes = new List<HtmlNode>(oldChild.ChildNodes.Cast<HtmlNode>()); nodes.Sort(new StreamPositionComparer()); foreach (HtmlNode grandchild in nodes) { parent.InsertAfter(grandchild, prev); } } parent.RemoveChild(oldChild); return oldChild; }
Simon mourier
source share