How to Remove All HTML Tags from a String in C#

If you’ve ever encountered a situation where you need to remove all HTML tags from a string, but you don’t know which tags are present, you’re in the right place. In this article, I will guide you through the process of removing HTML tags from a string using C#.

What is the Problem?

When working with text that contains HTML tags, you may sometimes need to extract the plain text without any HTML formatting. This is particularly useful when you want to display the content in a plain text format or perform further processing on the text.

The challenge arises when you don’t know which HTML tags are present in the string. In such cases, manually removing each tag becomes impractical and time-consuming. Therefore, we need a solution that can remove all HTML tags from the string, regardless of their type or quantity.

How to Remove HTML Tags Using Regular Expressions

One of the simplest and most efficient ways to remove HTML tags from a string is by using regular expressions. Regular expressions provide a powerful pattern-matching mechanism that allows us to search and replace specific patterns in a string.

In C#, you can use the Regex.Replace method to remove HTML tags from a string. Here’s an example of how you can implement this:

using System.Text.RegularExpressions;

public static string StripHTML(string input)
   return Regex.Replace(input, "<.*?>", String.Empty);

In the above code, the StripHTML method takes an input string and uses the Regex.Replace method to replace all occurrences of HTML tags with an empty string. The regular expression pattern <.*?> matches any HTML tag, including its attributes, and the String.Empty parameter is used to replace the matched tags with nothing.

Limitations of Using Regular Expressions

While using regular expressions to remove HTML tags can be a quick and convenient solution, it does have its limitations. Here are a few things to consider:

  1. Nested Tags: The regular expression pattern <.*?> matches the shortest possible tag, which means it may not handle nested tags correctly. For example, if you have a string like <b><i>Text</i></b>, the pattern will remove the outer <b> and <i> tags, leaving the inner <i> tag intact. To handle nested tags properly, you may need to use a more advanced regular expression pattern or consider an alternative solution.

  2. Security Concerns: Using regular expressions alone to sanitize user input or prevent cross-site scripting (XSS) attacks is not recommended. Regular expressions are not foolproof and can be bypassed by cleverly crafted input. If you’re dealing with user-generated content or security-sensitive data, it’s essential to use a more robust HTML parsing library or follow best practices for input validation and encoding.

Alternative Solution: HTML Agility Pack

If you need a more robust solution that can handle nested tags and provides better control over HTML parsing, you can consider using the HTML Agility Pack. The HTML Agility Pack is a popular open-source library for parsing and manipulating HTML documents.

To remove HTML tags using the HTML Agility Pack, you can follow these steps:

  1. Install the HTML Agility Pack NuGet package in your project.
  2. Import the HtmlAgilityPack namespace.
  3. Load the HTML string into an HtmlDocument object.
  4. Use the DocumentNode.DescendantsAndSelf method to iterate over all HTML elements.
  5. Extract the inner text of each element and concatenate them into a single string.

Here’s an example implementation using the HTML Agility Pack:

using HtmlAgilityPack;

public static string StripHTML(string input)
    var htmlDocument = new HtmlDocument();

    var plainText = string.Join(" ", htmlDocument.DocumentNode.DescendantsAndSelf()
        .Where(n => n.NodeType == HtmlNodeType.Text)
        .Select(n => n.InnerText.Trim()));

    return plainText;

In the above code, we load the HTML string into an HtmlDocument object and then use LINQ to iterate over all HTML elements. We filter out the elements of type HtmlNodeType.Text to extract only the plain text content. Finally, we concatenate the inner text of each element into a single string.

The HTML Agility Pack provides more flexibility and control over HTML parsing, making it a suitable choice for complex scenarios where regular expressions may fall short.

Remember to consider the specific requirements of your project and choose the solution that best fits your needs.

Categories C#

Related Posts

How to Post an Empty Body to REST API via HttpClient in C When working with REST APIs in C#, you may come across scenarios where you need to send a POST request with an empty body. This can be useful in situations where the API endpoint expects a POST request but doesn’t require any ...

Read more

Does C# have IsNullOrEmpty for List/IEnumerable?

If you’ve been working with C# for a while, you may have come across situations where you need to check if a List or IEnumerable is null or empty. In some other languages, like JavaScript, you have the convenience of using the Array.isArray() method or the length property to check if an array is empty ...

Read more

List Queries: 20 Times Faster than IQueryable?

Have you ever wondered why list queries are often faster than IQueryable queries in C#? In this article, we will explore the reasons behind this performance difference and discuss when and how to use each type of query. What is IQueryable? Before we dive into the performance comparison, let’s first understand what IQueryable is. IQueryable ...

Read more

Passing Different Number of Arguments to a Params Method at Runtime

How to Pass a Different Number of Arguments Using a List and Converting to an Array One way to pass a different number of arguments to a params method at runtime is by using a List<object> to store the arguments and then converting it to an array using the ToArray() method. Here’s an example: List<object> ...

Read more

Leave a Comment