Skip to content Skip to sidebar Skip to footer

Removing Commented Lines From Innertext

i'm currently using the below code which extracts the InnerText, however, what happens is i'm stuck with a bunch of comment out lines of html <-- how do I remove these using th

Solution 1:

Just filter the nodes by comment nodes and call remove on them.

var rootNode = doc.DocumentNode;
var query = rootNode.Descendants().OfType<HtmlCommentNode>().ToList();
foreach (var comment in query)
{
    comment.Remove();
}

Solution 2:

This is probably a better answer:

public static void RemoveComments(HtmlNode node)
{
    foreach (var n in node.ChildNodes.ToArray())
        RemoveComments(n);
    if (node.NodeType == HtmlNodeType.Comment)
        node.Remove();
}

Post a Comment for "Removing Commented Lines From Innertext"