Search code examples
c#.netunicodef#character-entities

Does .NET have built-in functions mapping between character entities and their unicode values?


& Eacute ;  \u00C9
& egrave ;  \u00E8
& eacute ;  \u00E9
& apos   ;  \u0027

something like:

f("'") = '\u0027' where f :: string -> char
g('\u0027') = "'" where g :: char -> string

Or is there a third-party library with a BSD or MIT style permissive free license with something of this sort? Otherwise I'll have to create my own mapping but it's quite urgent and I don't want to miss out on available functionality.


Solution

  • You can use the SecurityElement.Escape method to go from unicode to character entity:

    char c = '\u0027';
    string entity = System.Security.SecurityElement.Escape(c.ToString());
    Console.WriteLine(entity);
    

    As for going in reverse, I think you're out of luck and will need to write your own approach.

    EDIT: you might also find the HttpUtility.HtmlEncode and HttpUtility.HtmlDecode methods useful. To use them you'll need to add a reference to System.Web.