使用HTML Agility Pack查找和删除指定的HTML标签 [英] Find and remove specified HTML tags using Html Agility Pack

查看:90
本文介绍了使用HTML Agility Pack查找和删除指定的HTML标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使Html Agility Pack在我的情况下工作.我需要检测现有HTML页面中的所有脚本元素并将其删除,然后将更改保存到另一个文件中.在这里,bodyNode返回正确数量的脚本标签,但是我无法删除它们.新文件仍然具有这些标签.

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

推荐答案

您需要执行以下操作:

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

这篇关于使用HTML Agility Pack查找和删除指定的HTML标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆