Decode HTML escaped characters back to normal string in C#

16,061

Solution 1

use System.Web.HttpUtility.HtmlDecode or System.Net.WebUtility.HtmlDecode

var decoded = HttpUtility.HtmlDecode("< > &");

Solution 2

If you're using .NET 4.5 then you can use the HttpUtility.HtmlDecode method.

Solution 3

HttpUtility.UrlDecode("Your escaped String", System.Text.Encoding.Default);
Share:
16,061
Mario Stoilov
Author by

Mario Stoilov

Updated on June 26, 2022

Comments

  • Mario Stoilov
    Mario Stoilov almost 2 years

    My question is simple. I searched a little online, but could not find a quick way to unescape HTML text in a string.

    For example:
    "&lt; &gt; &amp;" should be returned to "< > &" as a string.

    Is there a quick way, or do I have to write my own unescaper?

  • Mario Stoilov
    Mario Stoilov about 11 years
    Is there a difference between these two?
  • P-L
    P-L over 10 years
    HttpUtility.HtmlDecode is available since .NET 1.1. msdn.microsoft.com/en-us/library/…
  • Hitesh
    Hitesh over 6 years
    it does not unescape &#0000106&#0000097&#0000118
  • Hitesh
    Hitesh over 6 years
    @jazzcat, did you find the solution?