HTML Entities Class
I could not find an easy way to get HTML Entity strings (e.g. "& g t ;" - without the spaces) based on the character value, so I created this easy HtmlEntities static class based on the Generic Dictionary.
There are many situations (such as blog posts, web.config entries and others) where it is necessary to find and use the HTML Entity value string for a given character. Rather than always have to look them up, as an experiment, I created this handy static HtmlEntities class.
It was an instructional experiment, since I started out with a NameValueCollection, only to find out that it can't handle case-sensitive keys. In addition, if NameValueCollection finds multiple entries, it returns them as a comma-delimited string.
By switching over to the Generic Dictionary class, I got what I wanted, and was able to quickly eliminate duplicates through a simple try / catch in the static constructor of the class.
On a side note, this is an example of where one might rightfully use a generic System.Exception catch in debugging new code - without wrapping the constructor in a try / catch block, it would be virtually impossible to tell which statement actually throws the type initializer exception. With the Debug.WriteLine(ex.Message +ex.StackTrace) code in the catch, the StackTrace will provide you with the exact line number of the offender:
using System;
using System.Collections.Generic;
public class HtmlEntities
{
public static Dictionary<string,string> Entities = new Dictionary<string,string>();
static HtmlEntities ()
{
try
{
//Entities.Add(""," ");
Entities.Add("!", "!");
// Entities.Add("\"",""");
Entities.Add("#", "#");
Entities.Add("$", "$");
Entities.Add("%", "%");
//Entities.Add("&","&");
Entities.Add("'", "'");
Entities.Add("(", "(");
Entities.Add(")", ")");
Entities.Add("*", "*");
Entities.Add("+", "+");
Entities.Add(",", ",");
Entities.Add("-", "-");
Entities.Add(".", ".");
Entities.Add("/", "/");
Entities.Add("0", "0");
Entities.Add("1", "1");
Entities.Add("2", "2");
Entities.Add("3", "3");
Entities.Add("4", "4");
Entities.Add("5", "5");
Entities.Add("6", "6");
Entities.Add("7", "7");
Entities.Add("8", "8");
Entities.Add("9", "9");
Entities.Add(":", ":");
Entities.Add(";", ";");
//Entities.Add("<","<");
Entities.Add("=", "=");
//Entities.Add(">",">");
Entities.Add("?", "?");
Entities.Add("@", "@");
Entities.Add("A", "A");
Entities.Add("B", "B");
Entities.Add("C", "C");
Entities.Add("D", "D");
Entities.Add("E", "E");
Entities.Add("F", "F");
Entities.Add("G", "G");
Entities.Add("H", "H");
Entities.Add("I", "I");
Entities.Add("J", "J");
Entities.Add("K", "K");
Entities.Add("L", "L");
Entities.Add("M", "M");
Entities.Add("N", "N");
Entities.Add("O", "O");
Entities.Add("P", "P");
Entities.Add("Q", "Q");
Entities.Add("R", "R");
Entities.Add("S", "S");
Entities.Add("T", "T");
Entities.Add("U", "U");
Entities.Add("V", "V");
Entities.Add("W", "W");
Entities.Add("X", "X");
Entities.Add("Y", "Y");
Entities.Add("Z", "Z");
Entities.Add("[", "[");
Entities.Add(@"\", "\");
Entities.Add("]", "]");
Entities.Add("^", "^");
Entities.Add("_", "_");
Entities.Add("`", "`");
Entities.Add("a", "a");
Entities.Add("b", "b");
Entities.Add("c", "c");
Entities.Add("d", "d");
Entities.Add("e", "e");
Entities.Add("f", "f");
Entities.Add("g", "g");
Entities.Add("h", "h");
Entities.Add("i", "i");
Entities.Add("j", "j");
Entities.Add("k", "k");
Entities.Add("l", "l");
Entities.Add("m", "m");
Entities.Add("n", "n");
Entities.Add("o", "o");
Entities.Add("p", "p");
Entities.Add("q", "q");
Entities.Add("r", "r");
Entities.Add("s", "s");
Entities.Add("t", "t");
Entities.Add("u", "u");
Entities.Add("v", "v");
Entities.Add("w", "w");
Entities.Add("x", "x");
Entities.Add("y", "y");
Entities.Add("z", "z");
Entities.Add("{", "{");
Entities.Add("|", "|");
Entities.Add("}", "}");
Entities.Add("~", "~");
Entities.Add("", "");
Entities.Add("€", "€");
Entities.Add("", "");
Entities.Add("‚", "‚");
Entities.Add("ƒ", "ƒ");
Entities.Add("„", "„");
Entities.Add("…", "…");
Entities.Add("†", "†");
Entities.Add("‡", "‡");
Entities.Add("ˆ", "ˆ");
Entities.Add("‰", "‰");
Entities.Add("Š", "Š");
Entities.Add("‹", "‹");
Entities.Add("Œ", "Œ");
Entities.Add("", "");
Entities.Add("Ž", "Ž");
Entities.Add("", "");
Entities.Add("", "");
Entities.Add("‘", "‘");
Entities.Add("’", "’");
Entities.Add("“", "“");
Entities.Add("”", "”");
Entities.Add("•", "•");
Entities.Add("–", "–");
Entities.Add("—", "—");
Entities.Add("˜", "˜");
Entities.Add("™", "™");
Entities.Add("š", "š");
Entities.Add("›", "›");
Entities.Add("œ", "œ");
Entities.Add("", "");
Entities.Add("ž", "ž");
Entities.Add("Ÿ", "Ÿ");
//Entities.Add(""," ");
Entities.Add("¡", "¡");
// Entities.Add("¢", "¢");
Entities.Add("£", "£");
Entities.Add("¤", "¤");
Entities.Add("¥", "¥");
Entities.Add("¦", "¦");
Entities.Add("§", "§");
Entities.Add("¨", "¨");
// Entities.Add("©", "©");
Entities.Add("ª", "ª");
Entities.Add("«", "«");
Entities.Add("¬", "¬");
Entities.Add("", "­");
Entities.Add("®", "®");
Entities.Add("¯", "¯");
//Entities.Add("°", "°");
Entities.Add("±", "±");
Entities.Add("²", "²");
Entities.Add("³", "³");
Entities.Add("´", "´");
Entities.Add("µ", "µ");
Entities.Add("¶", "¶");
// Entities.Add("·", "·");
Entities.Add("¸", "¸");
Entities.Add("¹", "¹");
Entities.Add("º", "º");
Entities.Add("»", "»");
Entities.Add("¼", "¼");
Entities.Add("½", "½");
Entities.Add("¾", "¾");
Entities.Add("¿", "¿");
Entities.Add("À", "À");
Entities.Add("Á", "Á");
Entities.Add("Â", "Â");
Entities.Add("Ã", "Ã");
Entities.Add("Ä", "Ä");
Entities.Add("Å", "Å");
Entities.Add("Æ", "Æ");
Entities.Add("Ç", "Ç");
Entities.Add("È", "È");
Entities.Add("É", "É");
Entities.Add("Ê", "Ê");
Entities.Add("Ë", "Ë");
Entities.Add("Ì", "Ì");
Entities.Add("Í", "Í");
Entities.Add("Î", "Î");
Entities.Add("Ï", "Ï");
Entities.Add("Ð", "Ð");
Entities.Add("Ñ", "Ñ");
Entities.Add("Ò", "Ò");
Entities.Add("Ó", "Ó");
Entities.Add("Ô", "Ô");
Entities.Add("Õ", "Õ");
Entities.Add("Ö", "Ö");
Entities.Add("×", "×");
Entities.Add("Ø", "Ø");
Entities.Add("Ù", "Ù");
Entities.Add("Ú", "Ú");
Entities.Add("Û", "Û");
Entities.Add("Ü", "Ü");
Entities.Add("Ý", "Ý");
Entities.Add("Þ", "Þ");
Entities.Add("ß", "ß");
Entities.Add("à", "à");
Entities.Add("á", "á");
Entities.Add("â", "â");
Entities.Add("ã", "ã");
Entities.Add("ä", "ä");
Entities.Add("å", "å");
Entities.Add("æ", "æ");
Entities.Add("ç", "ç");
Entities.Add("è", "è");
Entities.Add("é", "é");
Entities.Add("ê", "ê");
Entities.Add("ë", "ë");
Entities.Add("ì", "ì");
Entities.Add("í", "í");
Entities.Add("î", "î");
Entities.Add("ï", "ï");
Entities.Add("ð", "ð");
Entities.Add("ñ", "ñ");
Entities.Add("ò", "ò");
Entities.Add("ó", "ó");
Entities.Add("ô", "ô");
Entities.Add("õ", "õ");
Entities.Add("ö", "ö");
Entities.Add("÷", "÷");
Entities.Add("ø", "ø");
Entities.Add("ù", "ù");
Entities.Add("ú", "ú");
Entities.Add("û", "û");
Entities.Add("ü", "ü");
Entities.Add("ý", "ý");
Entities.Add("þ", "þ");
Entities.Add("ÿ", "ÿ");
Entities.Add("&", "&");
Entities.Add("¢", "¢");
Entities.Add("©","©");
Entities.Add("°", "°");
Entities.Add("·", "·");
Entities.Add(">", ">");
Entities.Add("<", "<");
Entities.Add(" ", " ");
Entities.Add("¬", "¬");
Entities.Add("¶", "¶");
Entities.Add("\"", """);
Entities.Add("®", "®");
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message + ex.StackTrace);
}
}
}
I've left the "offenders" in there, commented of course. The downloadable zip file contains a simple Console app that goes through all of them. To use this, all you need to do is pass the "key" to the static Entities Dictionary, and you get back the HTML entity value you need:
Console.WriteLine(HtmlEntities.Entities["&"]);