Find and remove specified HTML tags using Html Agility Pack

泄露秘密 提交于 2019-12-08 17:30:19

问题


I'm trying to get Html Agility Pack to work in my case. I need to detect all script elements in an existing HTML page and remove them, saving the changes to another file. Here, bodyNode returns the correct number of script tags, but I can't remove them. The new file still has those tags.

if (doc.DocumentNode != null)         
{
     var bodyNode = doc.DocumentNode.SelectNodes("//script");          
     if (bodyNode != null)             
     {
          bodyNode.Clear(); // clears the collection only                    
     } 

     doc.Save("some file");        
 }

回答1:


You need to do something like this:

foreach(HtmlNode node in bodyNode)
{
   node.ParentNode.RemoveChild(node);
}


来源:https://stackoverflow.com/questions/6333363/find-and-remove-specified-html-tags-using-html-agility-pack

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