Convert HTML to Plain Text

Prem Murmu on 5/10/2020 9:23:37 PM

Convert html to Plan text using Html Agilitypack.

1.We need to Install Nuget Package For HtmlAgilityPack.

2.Create blow class:

  public class HtmlToText

    {

        public HtmlToText()

        {

        }

        public string Convert(string path)

        {

            HtmlDocument doc = new HtmlDocument();

            doc.Load(path);

            StringWriter sw = new StringWriter();

            ConvertTo(doc.DocumentNode, sw);

            sw.Flush();

            return sw.ToString();

        }

        public string ConvertHtml(string html)

        {

            HtmlDocument doc = new HtmlDocument();

            doc.LoadHtml(html);

            StringWriter sw = new StringWriter();

            ConvertTo(doc.DocumentNode, sw);

            sw.Flush();

            return sw.ToString();

        }

        private void ConvertContentTo(HtmlNode node, TextWriter outText)

        {

            foreach (HtmlNode subnode in node.ChildNodes)

            {

                ConvertTo(subnode, outText);

            }

        }

        public void ConvertTo(HtmlNode node, TextWriter outText)

        {

            string html;

            switch (node.NodeType)

            {

                case HtmlNodeType.Comment:

                    // don't output comments

                    break;

                case HtmlNodeType.Document:

                    ConvertContentTo(node, outText);

                    break;


                case HtmlNodeType.Text:

                    // script and style must not be output

                    string parentName = node.ParentNode.Name;

                    if ((parentName == "script") || (parentName == "style"))

                        break;

                    // get text

                    html = ((HtmlTextNode)node).Text;


                    // is it in fact a special closing node output as text?

                    if (HtmlNode.IsOverlappedClosingElement(html))

                        break;

                    // check the text is meaningful and not a bunch of whitespaces

                    if (html.Trim().Length > 0)

                    {

                        outText.Write(HtmlEntity.DeEntitize(html));

                    }

                    break;

                case HtmlNodeType.Element:

                    switch (node.Name)

                    {

                        case "p":

                            // treat paragraphs as crlf

                            outText.Write("\r\n");

                            break;

                    }

                    if (node.HasChildNodes)

                    {

                        ConvertContentTo(node, outText);

                    }

                    break;

            }

        }

    }


3. We will create the object of  Above class like below.

     private  HtmlToText htmTxt = new HtmlToText();

4.  We will put the string html documents and string html doc object in CovertHml(string html) method.
    var plainText = htmTxt .ConvertHtml(string html);
Like:
    string Htmlobject="<h1 style="color:green"color:green">Hello World!</h1>";
    var plainText = htmTxt .ConvertHtml(string html);

Output:

Hello World!



0 Comments on this post

Comments(0)||Login to Comments



I've Been Vaccinated Wristband



work form home items,essentials,home office furnitures,laptops

afforable web hosting plans
Reliable Security & High Speed web hosting  plans
Microsoft Surface Book 3
InterServer Web Hosting and VPS
free .com domain and best cheap hosting plans
Premium Subscription Offer
  • see more..