437

I have email addresses encoded with HTML character entities. Is there anything in .NET that can convert them to plain strings?

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
Vasil
  • 36,468
  • 26
  • 90
  • 114

10 Answers10

671

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.

Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
  • 1
    It'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
  • 1
    It'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
  • 1
    I have using System.Web. In my context that namespace has only some AspPermission classes. – Vasil Sep 23 '08 at 18:23
  • 17
    Add 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
  • 11
    In 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
203

On .Net 4.0:

System.Net.WebUtility.HtmlDecode()

No need to include assembly for a C# project

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
Indy9000
  • 8,651
  • 2
  • 32
  • 37
46

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.

OwenP
  • 24,950
  • 13
  • 65
  • 102
16

If there is no Server context (i.e your running offline), you can use HttpUtility.HtmlDecode.

Rob Cooper
  • 28,567
  • 26
  • 103
  • 142
15

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
Patrick
  • 419
  • 4
  • 16
Abhishek Jaiswal
  • 1,161
  • 12
  • 6
12

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.

Hypershadsy
  • 398
  • 4
  • 13
7

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.

Daniel Schierbeck
  • 1,942
  • 2
  • 17
  • 24
  • 5
    There 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
1

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;
        }

    }
Tahir Alvi
  • 896
  • 2
  • 14
  • 44
1

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;
        }
    }
Vinod Srivastav
  • 3,644
  • 1
  • 27
  • 40
1

For strings containing &#x20; 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.

self.name
  • 2,341
  • 2
  • 17
  • 18