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:
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: