I want to remove all span elements (without attributes) but leave the inner html. I have created the following code snippet which appears to work but I can't help thinking this is overly complicated for such a task. Is there a better way?
var config = Configuration.Default.WithDefaultLoader().WithCss();
var parser = new HtmlParser(config);
var document = parser.Parse("<p><span><span><em>span text</em></span> </span> span text</p>");
foreach (var element in document.Descendents())
{
var parent = element.Parent;
while (parent != null)
{
var span = parent as IHtmlSpanElement;
if (span != null && !span.Attributes.Any())
{
span.Replace(span.ChildNodes.ToArray());
}
parent = parent.Parent;
}
}
document.Body.InnerHtml.Dump();
// outputs: <p><em>span text</em> span text</p>