remove html node from htmldocument :HTMLAgilityPack

前端 未结 4 608
闹比i
闹比i 2020-12-16 18:17

In my code, I want to remove the img tag which doesn\'t have src value. I am using HTMLAgilitypack\'s HtmlDocument object. I am finding the img whic

4条回答
  •  臣服心动
    2020-12-16 18:58

    var emptyImages = doc.DocumentNode
     .Descendants("img")
     .Where(x => x.Attributes["src"] == null || x.Attributes["src"].Value == String.Empty)
     .Select(x => x.XPath)
     .ToList(); 
    
    emptyImages.ForEach(xpath => { 
          var node = doc.DocumentNode.SelectSingleNode(xpath);
          if (node != null) { node.Remove(); }
        });
    

提交回复
热议问题