How I remove unwanted pre and code tags of html content with HtmlAgilityPack

c# html-agility-pack

Question

I created this based code, but how I remove all pre and code tags.

private static string RemoveHtmlTags(this string markup)
{
    if (string.IsNullOrEmpty(markup))
    {
        return string.Empty;
    }

    var document = new HtmlDocument();

    document.LoadHtml(markup);

    return document.DocumentNode.InnerText;
}
1
1
8/27/2018 7:00:00 PM

Accepted Answer

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

doc.DocumentNode.Descendants()
                .Where(n => n.Name == "script" || n.Name == "style")
                .ToList()
                .ForEach(n => n.Remove());
51
11/18/2012 3:31:58 PM

Popular Answer

You can do so using HtmlDocument class:

HtmlDocument doc = new HtmlDocument();

doc.LoadHtml(input);

doc.DocumentNode.SelectNodes("//style|//script").ToList().ForEach(n => n.Remove());


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