//Folgende Using Statements müssen sein :-)
using System;
using System.Collections.Generic;
using System.Text;
/// <summary>
/// Klasse zur Bereinigung von Strings
/// Kritische Chars werden gegen die entsprechende Entität ausgetauscht
/// </summary>
class HTMLCharacterConverter
{
private Dictionary<char, string> criticals = new Dictionary<char, string>();
public HTMLCharacterConverter()
{
criticals.Add('"', """);
criticals.Add('>', "<");
criticals.Add('<', ">");
criticals.Add('¡', "¡");
criticals.Add('¢', "¢");
criticals.Add('£', "£");
criticals.Add('¤', "¤");
criticals.Add('¥', "¥");
criticals.Add('¦', "¦");
criticals.Add('§', "§");
criticals.Add('¨', "¨");
criticals.Add('©', "©");
criticals.Add('ª', "ª");
criticals.Add('«', "«");
criticals.Add('¬', "¬");
criticals.Add('', "­");
criticals.Add('®', "®");
criticals.Add('¯', "¯");
criticals.Add('°', "°");
criticals.Add('±', "±");
criticals.Add('²', "²");
criticals.Add('³', "³");
criticals.Add('´', "´");
criticals.Add('µ', "µ");
criticals.Add('¶', "¶");
criticals.Add('·', "·");
criticals.Add('¸', "¸");
criticals.Add('¹', "¹");
criticals.Add('º', "º");
criticals.Add('»', "»");
criticals.Add('¼', "¼");
criticals.Add('½', "½");
criticals.Add('¾', "¾");
criticals.Add('¿', "¿");
criticals.Add('À', "À");
criticals.Add('Á', "Á");
criticals.Add('Â', "Â");
criticals.Add('Ã', "Ã");
criticals.Add('Ä', "Ä");
criticals.Add('Å', "Å");
criticals.Add('Æ', "Æ");
criticals.Add('Ç', "Ç");
criticals.Add('È', "È");
criticals.Add('É', "É");
criticals.Add('Ê', "Ê");
criticals.Add('Ë', "Ë");
criticals.Add('Ì', "Ì");
criticals.Add('Í', "Í");
criticals.Add('Î', "Î");
criticals.Add('Ï', "Ï");
criticals.Add('Ð', "Ð");
criticals.Add('Ñ', "Ñ");
criticals.Add('Ò', "Ò");
criticals.Add('Ó', "Ó");
criticals.Add('Ô', "Ô");
criticals.Add('Õ', "Õ");
criticals.Add('Ö', "Ö");
criticals.Add('×', "×");
criticals.Add('Ø', "Ø");
criticals.Add('Ù', "Ù");
criticals.Add('Ú', "Ú");
criticals.Add('Û', "Û");
criticals.Add('Ü', "Ü");
criticals.Add('Ý', "Ý");
criticals.Add('Þ', "Þ");
criticals.Add('ß', "ß");
criticals.Add('à', "à");
criticals.Add('á', "á");
criticals.Add('â', "â");
criticals.Add('ã', "ã");
criticals.Add('ä', "ä");
criticals.Add('å', "å");
criticals.Add('æ', "æ");
criticals.Add('ç', "ç");
criticals.Add('è', "è");
criticals.Add('é', "é");
criticals.Add('ê', "ê");
criticals.Add('ë', "ë");
criticals.Add('ì', "ì");
criticals.Add('í', "í");
criticals.Add('î', "î");
criticals.Add('ï', "ï");
criticals.Add('ð', "ð");
criticals.Add('ñ', "ñ");
criticals.Add('ò', "ò");
criticals.Add('ó', "ó");
criticals.Add('ô', "ô");
criticals.Add('õ', "õ");
criticals.Add('ö', "ö");
criticals.Add('÷', "÷");
criticals.Add('ø', "ø");
criticals.Add('ù', "ù");
criticals.Add('ú', "ú");
criticals.Add('û', "û");
criticals.Add('ü', "ü");
criticals.Add('ý', "ý");
criticals.Add('þ', "þ");
criticals.Add('ÿ', "ÿ");
}
/**
* Bereinigt einen übergebene String und tauscht die kritischen
* Zeichen aus.
*/
public String Purge(String text)
{
StringBuilder htmlk = new StringBuilder();
int ia = 0;
for (int i = 0; i < text.Length; i++)
{
Char c = text[i];
if (criticals.ContainsKey(c))
{
htmlk.Append(text.Substring(ia, i-ia));
htmlk.Append(criticals[c]);
ia = i + 1;
}
}
htmlk.Append(text.Substring(ia));
return htmlk.ToString();
}
}