Tuesday, 18 February 2014

How to remove script tags from html using html agility pack ,C# Dot Net



Description: To Remove unwanted Script tags, we need to search them using HtmlAgility Filter and then need to use remove function of node to delete them.

Customized method to delete all script tags in html document is as given below.

objHTMLdoc => is HTmlDocument object created using HTMLAgilityPack

public static HtmlDocument RemoveScripts(HtmlDocument objHTMLdoc)
        {
            HtmlNodeCollection nodes = objHTMLdoc.DocumentNode.SelectNodes("//script");
            if (nodes != null)
            {
                for (int Index = 0; Index < nodes.Count(); Index++)
                {
                    HtmlNode node = nodes[Index];
                    node.Remove();
                }
            }
            return objHTMLdoc;
        }


No comments:

Post a Comment