Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

c# - Remove all empty/unnecessary nodes from HTML

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]);
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...