What would be the preferred way to remove all empty and unnecessery nodes? For example
<p></p>
should be removed and <font><p><span><br></span></p></font>
should also be removed (so the br tag is considered unneccesery in this case)
Will I have to use some sort of recursive function for this? I'm thinking something along the lines of this maybe:
RemoveEmptyNodes(HtmlNode containerNode)
{
var nodes = containerNode.DescendantsAndSelf().ToList();
if (nodes != null)
{
foreach (HtmlNode node in nodes)
{
if (node.InnerText == null || node.InnerText == "")
{
RemoveEmptyNodes(node.ParentNode);
node.Remove();
}
}
}
}
But that obviously doesn't work (stackoverflow exception).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…