title: Remove all HTML tags and format text with returns, spaces, etc. using .NET
body: <p>I have an issue to strip HTML and show as customer formatted text.</p>
  
  <p>For example:</p>
  
  <pre><code>asdas&lt;br/&gt;asdas
  </code></pre>
  
  <p>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.</p>
  
  <p>Current solution (searching for better and done):</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Methods to remove HTML from strings.
  /// &lt;/summary&gt;
  public static class HtmlRemoval
  {
      /// &lt;summary&gt;
      /// Compiled regular expression for performance.
      /// &lt;/summary&gt;
      static Regex _htmlRegex = new Regex("&lt;.*?&gt;", RegexOptions.Compiled);
  
      /// &lt;summary&gt;
      /// Remove HTML from string with compiled Regex.
      /// &lt;/summary&gt;
      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("&lt;br/&gt;", Environment.NewLine)
              .Replace("&lt;/div&gt;", Environment.NewLine)
               .Replace("&lt;/p&gt;", Environment.NewLine);
      }
  }
  </code></pre>