Find and remove specified HTML tags using the Html Agility Pack

I am trying to get the Html Agility Pack to work in my case. I need to detect all script elements on an existing HTML page and delete them, saving the changes in another file. Here bodyNode returns the correct number of script tags, but I cannot 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"); } 
+7
source share
1 answer

You need to do something like this:

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

All Articles