Matthew
Matthew

Reputation: 25773

Decoding all HTML Entities

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.

Upvotes: 27

Views: 40826

Answers (2)

Pavel Donchev
Pavel Donchev

Reputation: 1899

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.

Upvotes: 38

John Gibb
John Gibb

Reputation: 10773

Use WebUtility.HtmlDecode included in .Net 4

For example, if I run in a console app:

  Console.WriteLine(WebUtility.HtmlDecode("&nbsp;, &amp;, &copy;"));

I get , &, c

Upvotes: 23

Related Questions