Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7832213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:05:39+00:00 2026-06-02T12:05:39+00:00

I have a Html code and I want to Convert it to plain text

  • 0

I have a Html code and I want to Convert it to plain text but keep only colored text tags.
for example:
when I have below Html:

<body>

This is a <b>sample</b> html text.
<p align="center" style="color:#ff9999">this is only a sample<p>
....
and some other tags...
</body>
</html>

I want the output:

this is a sample html text.
<#ff9999>this is only a sample<>
....
and some other tags...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T12:06:00+00:00Added an answer on June 2, 2026 at 12:06 pm

    I’d use parser to parse HTML like HtmlAgilityPack, and use regular expressions to find the color value in attributes.

    First, find all the nodes that contain style attribute with color defined in it by using xpath:

    var doc = new HtmlDocument();
    doc.LoadHtml(html);
    var nodes = doc.DocumentNode
        .SelectNodes("//*[contains(@style, 'color')]")
        .ToArray();
    

    Then the simplest regex to match a color value: (?<=color:\s*)#?\w+.

    var colorRegex = new Regex(@"(?<=color:\s*)#?\w+", RegexOptions.IgnoreCase);
    

    Then iterate through these nodes and if there is a regex match, replace the inner html of the node with html encoded tags (you’ll understand why a little bit later):

    foreach (var node in nodes)
    {
        var style = node.Attributes["style"].Value;
        if (colorRegex.IsMatch(style))
        {
            var color = colorRegex.Match(style).Value;
            node.InnerHtml =
                HttpUtility.HtmlEncode("<" + color + ">") +
                node.InnerHtml +
                HttpUtility.HtmlEncode("</" + color + ">");
        }
    }
    

    And finally get the inner text of the document and perform html decoding on it (this is because inner text strips all the tags):

    var txt = HttpUtility.HtmlDecode(doc.DocumentNode.InnerText);
    

    This should return something like this:

    This is a sample html text.
    <#ff9999>this is only a sample</#ff9999>
    ....
    and some other tags...
    

    Of course you could improve it for your needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an html code saved in a database. I want to convert specific
I have an HTML file and i want to convert to XML only using
I have an HTML file and i want to convert to XML document only
I have this html code that i want to edit with jQuery. Here is
I have asp.net generated html code. I want execute jQuery click function when user
I have below html code in my aspx. <input type=hidden id=medicalLink value='<a href=/forms/contactus.aspx >Contact
I have this html code <html> <head> <title>JQuery Problem 2</title> <script type=text/javascript src=jquery-1.4.min.js></script> <script
I'm trying to use a raphael.js example located here: http://raphaeljs.com/pie.html but I want to
I want to convert local html file on iPhone to PDF document. I have
I have a working Html.RouteLink that I want to convert to T4MVC. The working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.