Remove all empty/unnecessary nodes from HTML

落爺英雄遲暮 提交于 2019-11-28 05:14:43

问题


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).


回答1:


tags that should not be removed you can add the names to the list and nodes with attributes are also not removed because of containerNode.Attributes.Count == 0 (e.g. Images)

static List<string> _notToRemove;

static void Main(string[] args)
{
    _notToRemove = new List<string>();
    _notToRemove.Add("br");

    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml("<html><head></head><body><p>test</p><br><font><p><span></span></p></font></body></html>");
    RemoveEmptyNodes(doc.DocumentNode);
}

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


来源:https://stackoverflow.com/questions/11579035/remove-all-empty-unnecessary-nodes-from-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!