Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting HTML entities to Unicode Characters in C#

I found similar questions and answers for Python and Javascript, but not for C# or any other WinRT compatible language.

The reason I think I need it, is because I'm displaying text I get from websites in a Windows 8 store app. E.g. é should become é.

Or is there a better way? I'm not displaying websites or rss feeds, but just a list of websites and their titles.

like image 880
Remy Avatar asked Nov 21 '12 11:11

Remy


People also ask

Is HTML in Unicode?

An HTML document is a sequence of Unicode characters. More specifically, HTML 4.0 documents are required to consist of characters in the HTML document character set : a character repertoire wherein each character is assigned a unique, non-negative integer code point.

Is HTML a Unicode or Ascii?

The HTML5 Standard: Unicode UTF-8 The Unicode Standard covers (almost) all the characters, punctuations, and symbols in the world. Unicode enables processing, storage, and transport of text independent of platform and language. The default character encoding in HTML-5 is UTF-8.

What does Httputility HtmlDecode do?

HtmlDecode(String, TextWriter)Converts a string that has been HTML-encoded into a decoded string, and sends the decoded string to a TextWriter output stream.

What is HTML entity encoding?

HTML encoding converts characters that are not allowed in HTML into character-entity equivalents; HTML decoding reverses the encoding. For example, when embedded in a block of text, the characters < and > are encoded as &lt; and &gt; for HTTP transmission.


1 Answers

I recommend using System.Net.WebUtility.HtmlDecode and NOT HttpUtility.HtmlDecode.

This is due to the fact that the System.Web reference does not exist in Winforms/WPF/Console applications and you can get the exact same result using this class (which is already added as a reference in all those projects).

Usage:

string s =  System.Net.WebUtility.HtmlDecode("&eacute;"); // Returns é 
like image 178
Blachshma Avatar answered Oct 03 '22 12:10

Blachshma