Html Agility Pack allows you to find and delete certain HTML tags.

html-agility-pack

Question

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
7
3/22/2013 10:32:52 AM

Accepted Answer

You need to do something like this:

foreach(HtmlNode node in bodyNode)
{
   node.ParentNode.RemoveChild(node);
}
14
6/13/2011 5:45:44 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow