How to Decode HTML Entities in C

How can I decode HTML characters in C#?

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.

Decode HTML entities

To decode the string, use WebUtility.HtmlDecode.

Here's a sample LINQPad program that demonstrates:

void Main()
{
string s = "Feel";
string decoded = WebUtility.HtmlDecode(s);
decoded.Dump();
}

Output:

Feel

Note: You're missing a semicolon from the string you've presented in the question. Without the final semicolon, the output will be:

Feel

Decoding all HTML Entities

Then maybe you will need the HttpUtility.HtmlDecode?.
It should work, you just need to add a reference to System.Web.
At least this was the way in .Net Framework < 4.

For example the following code:

MessageBox.Show(HttpUtility.HtmlDecode("&©"));

Worked and the output was as expected (ampersand and copyright symbol).
Are you sure the problem is within HtmlDecode and not something else?

UPDATE: Another class capable of doing the job, WebUtility (again HtmlDecode method) came in the newer versions of .Net. However, there seem to be some problems with it. See the HttpUtility vs. WebUtility question.

How to decode HTML special character into their actual value?

Use HttpUtility.HtmlDecode or WebUtility.HtmlDecode

string s = "Antique bronze of an archer by Franz Iffland Literature:
“Bronzes, sculptors and founders” by H. Berman, Abage. 
“Dictionnaire illustré des sculpteurs animaliers & fondeurs de l’antiquité à nos jours “ by Jean Charles Hachet. Argus Valentines. 
“The dictionary of sculptors in bronze” by James Mackay. Antique collectors club. 
 Fedex shipping: $ 185";
var s2 = HttpUtility.HtmlDecode(s);
var s3 = WebUtility.HtmlDecode(s);

How do I decode html coded characters in an NSString?

Check out these NSString categories

iOS decode HTML entity to string

Both link below is working.
It is actually my careless mistake for missing out an character.

https://github.com/Koolistov/NSString-HTML

https://github.com/mwaterfall/MWFeedParser/blob/master/Classes/NSString%2BHTML.m

Windows tool to decode HTML entities in a file

You don't need extensive applications (like JREPL.bat or my own FindRepl.bat) or complicated programs in order to perform a replacement as simple as this one. The small Batch file below is an example that performs a replacement of 3 HTML entities:

@set @a=0 // & cscript //nologo //E:JScript "%~F0" < input.txt & goto :EOF

var rep = new Array();
rep["©"] = "\u00A9";
rep["팆"] = "\uD306";
rep["☃"] = "\u2603";

var f = new ActiveXObject("Scripting.FileSystemObject").CreateTextFile("output.txt", true, true);
f.Write(WScript.Stdin.ReadAll().replace(/©|팆|☃/g,function (A) {return rep[A]}));
f.Close();

input.txt:

Foo © bar 팆 baz ☃ qux

output.txt:

Foo © bar 팆 baz ☃ qux

You only need to add as many character equivalences as you want to convert...



Related Topics



Leave a reply



Submit