HTML Agility Pack RemoveChild - not working as expected

Let's say I want to remove the span tag from this html:

<html><span>we do like <b>bold</b> stuff</span></html> 

I expect this piece of code to do what I am after

 string html = "<html><span>we do like <b>bold</b> stuff</span></html>"; HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); HtmlNode span = doc.DocumentNode.Descendants("span").First(); span.ParentNode.RemoveChild(span, true); //second parameter is 'keepGrandChildren' 

But the conclusion is as follows:

 <html> stuff<b>bold</b>we do like </html> 

It seems that the reversal of child nodes is within range. Am I doing something wrong?

+6
source share
3 answers

Looks like an error in HtmlAgilityPack - see the problem log:

http://htmlagilitypack.codeplex.com/workitem/9113

Interestingly, this was raised 4 years ago ...

Here is a snippet that will remove all span tags (or any other tag that you specify) and save the other nodes in the correct order.

 void Main() { string html = "<html><span>we do like <b>bold</b> stuff</span></html>"; HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); RemoveTags(doc, "span"); Console.WriteLine(doc.DocumentNode.OuterHtml); } public static void RemoveTags(HtmlDocument html, string tagName) { var tags = html.DocumentNode.SelectNodes("//" + tagName); if (tags!=null) { foreach (var tag in tags) { if (!tag.HasChildNodes) { tag.ParentNode.RemoveChild(tag); continue; } for (var i = tag.ChildNodes.Count - 1; i >= 0; i--) { var child = tag.ChildNodes[i]; tag.ParentNode.InsertAfter(child, tag); } tag.ParentNode.RemoveChild(tag); } } } 
+11
source
 foreach (HtmlNode child in tag.ChildNodes) { tag.ParentNode.InsertBefore(child, tag); } tag.Remove(); 
+4
source

For records only, this is my version based on the answers to this question:

 using HtmlAgilityPack; internal static class HtmlAgilityPackExtensions { public static void RemoveNodeKeepChildren(this HtmlNode node) { foreach (var child in node.ChildNodes) { node.ParentNode.InsertBefore(child, node); } node.Remove(); } } 
+3
source

Source: https://habr.com/ru/post/900172/


All Articles