Remove tags, but keep the text?

c# html-agility-pack

Question

I'm using HtmlAgilityPack to parse a string that contains HTML code. What I'm trying to do is remove the <span> tags, but keep the content between them. Here's my code, so far:

var text = @"<div><span>This is some text.</span>More text here.</div>
<div>I have a question.<span>Hi</span></div>";
var doc = new HtmlDocument();
doc.LoadHtml(text);
var nodes = doc.DocumentNode.SelectNodes("//span");
foreach (HtmlNode node in nodes)
{
   node.Remove();
}
String result = doc.DocumentNode.InnerHtml;

The problem is, the result is missing "This is some text." - the text between the <span> tags. How can I accomplish what I'm trying to do? I realize that node.Remove actually removes the entire node, I was just hoping that there is a way I can say remove.Node, but keep the content.

Accepted Answer

Remove that node from parent while keeping the grandchildren.

foreach (var node in nodes)
{
    node.ParentNode.RemoveChild(node,true);
}


Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow