Tuesday, 18 February 2014

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



Description: To Remove unwanted Style 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 style tags in html document is as given below.

Doc => is HTmlDocument object created using HTMLAgilityPack

public static HtmlDocument removeUnwantedStyles(HtmlDocument doc)
        {
            int mStyleIndex = 0;
            try
            {
                HtmlNodeCollection nc = doc.DocumentNode.SelectNodes("//style");
                if (nc != null)
                {
                    for (mStyleIndex = 0; mStyleIndex < nc.Count(); mStyleIndex++)
                    {
                        HtmlNode node = nc[mStyleIndex];
                        node.Remove();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return doc;
        }


No comments:

Post a Comment