Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to strip comments from HTML using Agility Pack without losing DOCTYPE

I am trying to remove unnecessary content from HTML. Specifically I want to remove comments. I found a pretty good solution (Grabbing meta-tags and comments using HTML Agility Pack) however the DOCTYPE is treated as a comment and therefore removed along with the comments. How can I improve the code below to make sure the DOCTYPE is preserved?

var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(htmlContent);
var nodes = htmlDoc.DocumentNode.SelectNodes("//comment()");
if (nodes != null)
{
    foreach (HtmlNode comment in nodes)
    {
        comment.ParentNode.RemoveChild(comment);
    }
}
like image 393
desautelsj Avatar asked Jul 04 '11 05:07

desautelsj


1 Answers

doc.DocumentNode.Descendants()
 .Where(n => n.NodeType == HtmlAgilityPack.HtmlNodeType.Comment)
 .ToList()
 .ForEach(n => n.Remove());

this will strip off all comments from the document

like image 118
BlueBird Avatar answered Oct 22 '22 04:10

BlueBird