I have email addresses encoded with HTML character entities. Is there anything in .NET that can convert them to plain strings?
10 Answers
You can use HttpUtility.HtmlDecode
If you are using .NET 4.0+ you can also use WebUtility.HtmlDecode
which does not require an extra assembly reference as it is available in the System.Net
namespace.

- 81,193
- 14
- 123
- 132
-
1It's supposed to be in System.Web, but it isn't. I haven't touched C# for more that a year, if I get a bit more frustrated with this I'll convert them manually. – Vasil Sep 23 '08 at 18:10
-
1It's in the .NET 2.0 version of System.Web – Mark Cidade Sep 23 '08 at 18:14
-
I'm using exactly 2.0, but I'm writing a console app. – Vasil Sep 23 '08 at 18:15
-
you can import the system.web just like I can import the system.console if I were in a web app. It works. – SpoiledTechie.com Sep 23 '08 at 18:17
-
1I have using System.Web. In my context that namespace has only some AspPermission classes. – Vasil Sep 23 '08 at 18:23
-
17Add a reference to System.Web.Dll in your project properties. The classes you see live in System.dll which is referenced by default. – OwenP Sep 23 '08 at 18:26
-
11In case you're trying trying to decode the Query String, you need to use `HttpUtility.UrlDecode` – PeterX May 23 '13 at 08:10
-
@PeterX that's exactly what I needed. For future reference, should you ever need to reverse `encodeURI(stringToEncode)` you'll actually need `HttpUtility.UrlDecode(encodedString)`. – GigiSan Jan 25 '16 at 16:24
-
does not decode é none of the answers do. HtmlUtility/WebUtility cannot do this. – jazzcat Feb 12 '16 at 16:54
-
I am using System.Net.WebUtility.HtmlDecode() for decoding the string (hello & text). But the thing is I am using antixssencoder.htmlencode() before decoding it. So after decoding the text: (hello & text) I am getting "hello & text" as output. Please help me if I am doing wrong anywhere :-) – Aman Gupta Aug 17 '20 at 10:52
On .Net 4.0:
System.Net.WebUtility.HtmlDecode()
No need to include assembly for a C# project

- 43,651
- 22
- 107
- 170

- 8,651
- 2
- 32
- 37
-
8It is better solution because HttpUtility doesn't decode "'" symbol.. I don't know why.. – RredCat Sep 13 '11 at 13:44
-
This is required in developing for the Universal Windows platform. – matthewsheets Jun 10 '15 at 19:32
-
As @CQ says, you need to use HttpUtility.HtmlDecode, but it's not available in a non-ASP .NET project by default.
For a non-ASP .NET application, you need to add a reference to System.Web.dll
. Right-click your project in Solution Explorer, select "Add Reference", then browse the list for System.Web.dll
.
Now that the reference is added, you should be able to access the method using the fully-qualified name System.Web.HttpUtility.HtmlDecode
or insert a using
statement for System.Web
to make things easier.

- 24,950
- 13
- 65
- 102
If there is no Server context (i.e your running offline), you can use HttpUtility.HtmlDecode.

- 28,567
- 26
- 103
- 142
To decode HTML take a look below code
string s = "Svendborg Værft A/S";
string a = HttpUtility.HtmlDecode(s);
Response.Write(a);
Output is like
Svendborg Værft A/S

- 419
- 4
- 16

- 1,161
- 12
- 6
It is also worth mentioning that if you're using HtmlAgilityPack like I was, you should use HtmlAgilityPack.HtmlEntity.DeEntitize()
. It takes a string
and returns a string
.

- 398
- 4
- 13
Use Server.HtmlDecode
to decode the HTML entities. If you want to escape the HTML, i.e. display the <
and >
character to the user, use Server.HtmlEncode
.

- 1,942
- 2
- 17
- 24
-
5There may not be a server context (i.e. when running test cases and the like) I fell in to this trap before :) – Rob Cooper Sep 23 '08 at 18:04
Write static a method into some utility class, which accept string as parameter and return the decoded html string.
Include the using System.Web.HttpUtility
into your class
public static string HtmlEncode(string text)
{
if(text.length > 0){
return HttpUtility.HtmlDecode(text);
}else{
return text;
}
}

- 896
- 2
- 14
- 44
For .net 4.0
Add a reference to System.net.dll
to the project with using System.Net;
then use the following extensions
// Html encode/decode
public static string HtmDecode(this string htmlEncodedString)
{
if(htmlEncodedString.Length > 0)
{
return System.Net.WebUtility.HtmlDecode(htmlEncodedString);
}
else
{
return htmlEncodedString;
}
}
public static string HtmEncode(this string htmlDecodedString)
{
if(htmlDecodedString.Length > 0)
{
return System.Net.WebUtility.HtmlEncode(htmlDecodedString);
}
else
{
return htmlDecodedString;
}
}

- 3,644
- 1
- 27
- 40
For strings containing   I've had to double-decode the string. First decode would turn it into the second pass would correctly decode it to the expected character.

- 2,341
- 2
- 17
- 18