I'm trying to remove empty html nodes with HtmlAgilityPack. I want to remove all nodes like this:
<p><span> </span></p>
Here's what I'm trying but it's not working:
static string RemoveEmptyParagraphs(string html)
{
HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(html);
foreach (HtmlNode eachNode in document.DocumentNode.SelectNodes("//p/span/text() = ' '"))
eachNode.Remove();
html = document.DocumentNode.OuterHtml;
return html;
}
foreachbut rather a backwardforloop, because that is the only proper way to delete items from a list. Second, try changing the XPath string to"//p/span[text() = ' ']"or"//p/span[contains(text() = ' ')]"if you expect any spaces to appear in source HTML.