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 6018581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:17:45+00:00 2026-05-23T03:17:45+00:00

I have an issue to strip HTML and show as customer formatted text. For

  • 0

I have an issue to strip HTML and show as customer formatted text.

For example:

asdas<br/>asdas

So the tag will be replaced by a margin. But I also need to replace margins by spaces and tabs and remove all tags. Are there any examples or done solutions to get just somehow formatted text after HTML tags removal.

Current solution (searching for better and done):

/// <summary>
/// Methods to remove HTML from strings.
/// </summary>
public static class HtmlRemoval
{
    /// <summary>
    /// Compiled regular expression for performance.
    /// </summary>
    static Regex _htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);

    /// <summary>
    /// Remove HTML from string with compiled Regex.
    /// </summary>
    public static string StripAllTagsRegex(string source)
    {
        source = HttpUtility.HtmlEncode(source);
        return _htmlRegex.Replace(source, string.Empty);
    }

    public static string ChangeTagsToTextFormat(string source)
    {
        if (string.IsNullOrEmpty(source))
            return source;

        source = HttpUtility.HtmlEncode(source);
        return source.Replace("<br/>", Environment.NewLine)
            .Replace("</div>", Environment.NewLine)
             .Replace("</p>", Environment.NewLine);
    }
}
  • 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-05-23T03:17:45+00:00Added an answer on May 23, 2026 at 3:17 am

    I believe HTML Agility Pack is the simplest solution here, especially since your removing (possibly malformed) Html tags. The idea behind the following code is you just take all the nodes, return their InnerText along with a line break (“\n”, or whatever formatting you want to do, since you’ll have a Collection to work with after using SelectNodes):

        private string stripTags(string html)
        {
            var output = new StringBuilder();
            HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
    
            doc.LoadHtml(html);
    
            foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//*"))
            {
                output.AppendLine(node.InnerText + Environment.NewLine);
            }
    
            return output.ToString();
        }
    

    To get more specific formatting results, simply use different XPath expressions with the SelectNodes method. (The code presented here not actually tested, and you’ll probably want something a little more precise)

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

Sidebar

Related Questions

I have been adding items to tool strip by programming but the issue is
I have the following HTML page: sample_page.html: <h1>sample html page</h1> <script type=text/javascript src=sample_page.js/> The
I have an issue with my WYSIWYG editor. If users copy in outside text,
I have issue that is reproduced on g++. VC++ doesn't meet any problems. So
I have an issue that is driving me a bit nuts: Using a UserProfileManager
We have an issue using the PEAR libraries on Windows from PHP . Pear
We have an issue related to a Java application running under a (rather old)
We have an issue on our page whereby the first time a button posts
I have some issue with a Perl script. It modifies the content of a
I have an issue with using AWK to simply remove a field from a

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.