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.
Remove that node from parent while keeping the grandchildren.
foreach (var node in nodes)
{
node.ParentNode.RemoveChild(node,true);
}