问题
System.Net.WebUtility.HtmlDecode("€"); // returns €
System.Net.WebUtility.HtmlEncode("€"); // also returns €
How can I convert € (or any other currency symbol) to corresponding html entity.
In this example € => €
I'm using .Net 4.6.1
回答1:
HtmlEncode
only looks for a few special characters and replaces them with hard-coded values, and additionally a few more high ASCII characters (160 - 255) as described here. The only way to encode into entity names is by specifying them manually. I gave it a shot and built a wrapper around the System.Net.WebUtility
class while leveraging the existing Html entities dataset used by .NET to decode as well so that decoding continues to work with this solution. I've hosted it on github: WebUtilityWrapper. You can use it as shown below:
WebUtilityWrapper.HtmlEncode("€"); // Returns €
WebUtilityWrapper.HtmlEncode("Δ"); // Returns Δ
WebUtilityWrapper.HtmlEncode("&"); // Returns &
WebUtilityWrapper.HtmlEncode("$"); // Returns $
WebUtilityWrapper.HtmlEncode("€¢£¥"); // Returns €¢£¥
I've tested it out by encoding and decoding back and then verifying if we get the original string for a large set of unicode characters. Sharing some more tests:
HtmlEncode() Response using framework's HtmlEncode: (link)
// Alphabets
$+0123456789<=>ABCDEFGHIJKLMNOPQRSTUVWXYZ^`abcdefghijklmnopqrstuvwxyz|~
// Unicode 162 to 254
¢£¤¥¦§¨©ª¬®¯°
±´µ¶¸ºÀÁÂÃÄÅÆ
ÇÈÉÊËÌÍÎÏÐÑÒÓ
ÔÕÖ×ØÙÚÛÜÝÞßà
áâãäåæçèéêëìí
îïðñòóôõö÷øùú
ûüýþ
// Unicodes for Greek Alphabet
ΑΒΓΔΕΖΗΘΙΚΛΜΝ
// Unicodes for 9824 - 9830
♠♣♥♦
HtmlEncode() Response using WebUtilityWrapper.HtmlEncode:
// Alphabets
$+0123456789<=>ABCDEFGHIJKLMNOPQRSTUVWXYZ^`abcdefghijklmnopqrstuvwxyz|~
// Unicode 162 to 254
¢£¤¥¦§¨©ª¬®¯°
±´µ¶¸ºÀÁÂÃÄÅÆ
ÇÈÉÊËÌÍÎÏÐÑÒÓ
ÔÕÖ×ØÙÚÛÜÝÞßà
áâãäåæçèéêëìí
îïðñòóôõö÷øùú
ûüýþ
// Unicodes for Greek alphabet
ΑΒΓΔΕΖΗΘΙΚΛΜΝ
ΞΟΠΡΣΤΥΦΧΨΩ
// Unicodes for 9824 - 9830
♠♣♥♦
Hope this helps!
回答2:
HtmlEncode
and HtmlDecode
are not symmetric.
HtmlEncode
will only encode a handful of special characters:
<
=><
>
=>>
&
=>&
'
=>'
"
=>"
and for some unicode characters it will convert them to the &#<num>;
format.
So no conversions into HtmlEntities takes place.
来源:https://stackoverflow.com/questions/43717647/how-to-convert-currency-symbol-to-corresponding-html-entity