HTMLEncode and HTMLDecode are common methods which can be in HttpUtility or WebUtility to encode and decode html tags or templates in your web application. Usually, HTML templates are updated by the Admin which uses the same system with admin rights.
But what if the application you develop is accessing different databases that are updated from various admin portals or platforms. Hence the templates may contain many tags or characters and the above HTMLDecode will not work for them.
Following method represents the conversion of those special characters to the HTML Encoded values so that it can be understood by the browser to show the correct symbol.
To do so, you will need to first get the ASCII of each of the character and if the values falls in to a special character, replace it with a properly formatted string. Simply it looks like below;
public static string GetSpecialCharactersEncoded(string text)
{
if (text == null)
return text;
string returnString = text;
Dictionary<int, string> dicList = new Dictionary<int, string>();
StringBuilder sb = new StringBuilder();
foreach (var c in text.ToCharArray())
{
sb.Append(GetEncodedValue(c.ToString()));
}
return sb.ToString();
}
private static string GetEncodedValue(string text)
{
string encTxt = text;
int iAscii = (int)Convert.ToChar(text);
if (iAscii == 8230)
encTxt = "…";
else if (iAscii == 8211)
encTxt = "–";
else if (iAscii == 8212)
encTxt = "—";
else if (iAscii == 8216)
encTxt = "‘ ";
else if (iAscii == 8217)
encTxt = "’";
else if (iAscii == 8218)
encTxt = "‚";
else if (iAscii == 8220)
encTxt = "“ ";
else if (iAscii == 8221)
encTxt = "”";
else if (iAscii == 8222)
encTxt = "„";
else if (iAscii == 8224)
encTxt = "†";
else if (iAscii == 8225)
encTxt = "‡";
else if (iAscii == 8226)
encTxt = "•";
else if (iAscii == 8240)
encTxt = "‰";
else if (iAscii == 8364)
encTxt = "€";
else if (iAscii == 8482)
encTxt = "™";
else if (iAscii == 402)
encTxt = "ƒ";
else if (iAscii == 376)
encTxt = "ÿ";
else if (iAscii == 353)
encTxt = "š";
else if (iAscii == 352)
encTxt = "Š";
else if (iAscii == 339)
encTxt = "œ";
else if (iAscii == 338)
encTxt = "Œ";
else if (iAscii == 255)
encTxt = "ÿ";
else if (iAscii == 254)
encTxt = "þ";
else if (iAscii == 253)
encTxt = "ý";
else if (iAscii == 252)
encTxt = "ü";
else if (iAscii == 251)
encTxt = "û";
else if (iAscii == 250)
encTxt = "ú";
else if (iAscii == 249)
encTxt = "ù";
else if (iAscii == 248)
encTxt = "ø";
else if (iAscii == 247)
encTxt = "÷";
else if (iAscii == 246)
encTxt = "ö";
else if (iAscii == 245)
encTxt = "õ";
else if (iAscii == 244)
encTxt = "ô";
else if (iAscii == 243)
encTxt = "ó";
else if (iAscii == 242)
encTxt = "ò";
else if (iAscii == 241)
encTxt = "ñ";
else if (iAscii == 240)
encTxt = "ð";
else if (iAscii == 239)
encTxt = "ï";
else if (iAscii == 238)
encTxt = "î";
else if (iAscii == 237)
encTxt = "í";
else if (iAscii == 236)
encTxt = "ì";
else if (iAscii == 235)
encTxt = "ë";
else if (iAscii == 234)
encTxt = "ê";
else if (iAscii == 233)
encTxt = "é";
else if (iAscii == 232)
encTxt = "è";
else if (iAscii == 231)
encTxt = "ç";
else if (iAscii == 230)
encTxt = "æ";
else if (iAscii == 229)
encTxt = "å";
else if (iAscii == 228)
encTxt = "ä";
else if (iAscii == 227)
encTxt = "ã";
else if (iAscii == 226)
encTxt = "â";
else if (iAscii == 225)
encTxt = "á";
else if (iAscii == 224)
encTxt = "à";
else if (iAscii == 223)
encTxt = "ß";
else if (iAscii == 222)
encTxt = "Þ";
else if (iAscii == 221)
encTxt = "Ý";
else if (iAscii == 220)
encTxt = "Ü";
else if (iAscii == 219)
encTxt = "Û";
else if (iAscii == 218)
encTxt = "Ú";
else if (iAscii == 217)
encTxt = "Ù";
else if (iAscii == 216)
encTxt = "Ø";
else if (iAscii == 215)
encTxt = "×";
else if (iAscii == 214)
encTxt = "Ö";
else if (iAscii == 213)
encTxt = "Õ";
else if (iAscii == 212)
encTxt = "Ô";
else if (iAscii == 211)
encTxt = "Ó";
else if (iAscii == 210)
encTxt = "Ò";
else if (iAscii == 209)
encTxt = "Ñ";
else if (iAscii == 208)
encTxt = "Ð";
else if (iAscii == 207)
encTxt = "Ï";
else if (iAscii == 206)
encTxt = "Î";
else if (iAscii == 205)
encTxt = "Í";
else if (iAscii == 204)
encTxt = "Ì";
else if (iAscii == 203)
encTxt = "Ë";
else if (iAscii == 202)
encTxt = "Ê";
else if (iAscii == 201)
encTxt = "É";
else if (iAscii == 200)
encTxt = "È";
else if (iAscii == 199)
encTxt = "Ç";
else if (iAscii == 198)
encTxt = "Æ";
else if (iAscii == 197)
encTxt = "Å";
else if (iAscii == 196)
encTxt = "Ä";
else if (iAscii == 195)
encTxt = "Ã";
else if (iAscii == 194)
encTxt = "Â";
else if (iAscii == 193)
encTxt = "Á";
else if (iAscii == 192)
encTxt = "À";
else if (iAscii == 191)
encTxt = "¿";
else if (iAscii == 190)
encTxt = "¾";
else if (iAscii == 189)
encTxt = "½";
else if (iAscii == 188)
encTxt = "¼";
else if (iAscii == 187)
encTxt = "»";
else if (iAscii == 186)
encTxt = "º";
else if (iAscii == 185)
encTxt = "¹";
else if (iAscii == 184)
encTxt = "¸";
else if (iAscii == 183)
encTxt = "·";
else if (iAscii == 182)
encTxt = "¶";
else if (iAscii == 181)
encTxt = "µ";
else if (iAscii == 180)
encTxt = "´";
else if (iAscii == 179)
encTxt = "³";
else if (iAscii == 178)
encTxt = "²";
else if (iAscii == 177)
encTxt = "±";
else if (iAscii == 176)
encTxt = "°";
else if (iAscii == 175)
encTxt = "¯";
else if (iAscii == 174)
encTxt = "®";
else if (iAscii == 173)
encTxt = "­";
else if (iAscii == 172)
encTxt = "¬";
else if (iAscii == 171)
encTxt = "«";
else if (iAscii == 170)
encTxt = "ª";
else if (iAscii == 169)
encTxt = "©";
else if (iAscii == 168)
encTxt = "¨";
else if (iAscii == 167)
encTxt = "§";
else if (iAscii == 166)
encTxt = "¦";
else if (iAscii == 165)
encTxt = "¥";
else if (iAscii == 164)
encTxt = "¤";
else if (iAscii == 163)
encTxt = "£";
else if (iAscii == 162)
encTxt = "¢";
else if (iAscii == 161)
encTxt = "¡";
else if (iAscii == 160)
encTxt = " ";
else if (iAscii == 34)
encTxt = """;
return encTxt;
}
To call this method;
string res = GetSpecialCharactersEncoded(str);
For further reading, visit here (http://www.ascii.cl/htmlcodes.htm)
string res = GetSpecialCharactersEncoded(str);
For further reading, visit here (http://www.ascii.cl/htmlcodes.htm)
The character entity reference chart is here. (https://dev.w3.org/html5/html-author/charref)
Using this method will show the special characters without any issues.
Happy coding... :)
No comments:
Post a Comment