Skip to content Skip to sidebar Skip to footer

How To Remove Empty Html Nodes With HtmlAgilityPack?

I'm trying to remove empty html nodes with HtmlAgilityPack. I want to remove all nodes like this:

 

Here's what I'm trying but

Solution 1:

Before loading the html with document.LoadHtml(html); you can do this:

document.LoadHtml(html.Replace("<p><span>&nbsp;</span></p>", ""));

Or have a look at this:

static void RemoveEmptyNodes(HtmlNode containerNode)
{
  if (containerNode.Attributes.Count == 0 && !_notToRemove.Contains(containerNode.Name) && (containerNode.InnerText == null || containerNode.InnerText == string.Empty) )
  {
    containerNode.Remove();
  }
  else
  {
    for (int i = containerNode.ChildNodes.Count - 1; i >= 0; i-- )
    {
        RemoveEmptyNodes(containerNode.ChildNodes[i]);
    }
  }
}

Post a Comment for "How To Remove Empty Html Nodes With HtmlAgilityPack?"