Paste2 Logo
  1. using System.Collections.Specialized;
  2.  
  3. // ...
  4.  
  5. class HTMLEntityManagement
  6.     {
  7.         //Declare a NameValue collection to hold the elements
  8.         private NameValueCollection elementDictionary = new NameValueCollection();
  9.  
  10.         /// <summary>
  11.         /// Removes HTML entities and replaces them with their actual character symbol
  12.         /// </summary>
  13.         /// <param name="input">HTML to be stripped</param>
  14.         /// <returns>Stripped HTML containing character symbols instead of safe-elements</returns>
  15.         public string stripEntities(String input)
  16.         {
  17.             for (int i = 0; i <= this.elementDictionary.Count - 1; i++)
  18.             {
  19.                 string value = this.elementDictionary[i];
  20.                 string key = this.elementDictionary.Keys[i];
  21.  
  22.                 input = input.Replace(key, value);
  23.             }
  24.  
  25.             return input;
  26.         }
  27.  
  28.         /// <summary>
  29.         /// Replaces un-safe characters with HTML-safe entities
  30.         /// </summary>
  31.         /// <param name="input">HTML to be made safe</param>
  32.         /// <returns>Safe HTML</returns>
  33.         public string addEntities(String input)
  34.         {
  35.             for (int i = 0; i <= this.elementDictionary.Count - 1; i++)
  36.             {
  37.                 string value = this.elementDictionary[i];
  38.                 string key = this.elementDictionary.Keys[i];
  39.  
  40.                 input = input.Replace(value, key);
  41.             }
  42.  
  43.             return input;
  44.         }
  45.  
  46.         public HTMLEntityManagement()
  47.         {
  48.             /* Add any HTML elements, as so:
  49.              *
  50.              *  Key:      HTML element
  51.              *  Value:    Character symbol
  52.             */
  53.  
  54.             this.elementDictionary.Add("&quot;", @"""");
  55.             this.elementDictionary.Add("&amp;", "&");
  56.             this.elementDictionary.Add("&apos;", "'"); //XML/XHTML ONLY
  57.             this.elementDictionary.Add("&lt;", "<");
  58.             this.elementDictionary.Add("&gt;", ">");
  59.             this.elementDictionary.Add("&nbsp;", " ");
  60.             this.elementDictionary.Add("&iexclp;", "¡");
  61.             this.elementDictionary.Add("&cent;", "¢");
  62.             this.elementDictionary.Add("&pound;", "£");
  63.             this.elementDictionary.Add("&curren;", "¤");
  64.             this.elementDictionary.Add("&yen;", "¥");
  65.             this.elementDictionary.Add("&copy;", "©");
  66.             this.elementDictionary.Add("&reg;", "®");
  67.             this.elementDictionary.Add("&micro;", "µ");
  68.             this.elementDictionary.Add("&ordm;", "º");
  69.             this.elementDictionary.Add("&middot;", "·");
  70.             //...
  71.         }
  72.     }

C#: HTML Safe Entity Management - http://www.glassoforange.co.uk/?p=7