Decoding all HTML Entities

Matthew picture Matthew · Dec 1, 2011 · Viewed 36k times · Source

I'm looking for some function that will decode a good amount of HTML entities.

Reason is I am working on some code to take HTML content and turning it into plain text, the issue that I have is a lot of entities do not get converted using HttpUtility.HtmlDecode.

Some examples of entities I'm concerned about are  , &, ©.

This is for .net 3.5.

Answer

Pavel Donchev picture Pavel Donchev · Dec 1, 2011

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("&amp;&copy;"));

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.