Encode Value as HTML - CSharp System

CSharp examples for System:String HTML

Description

Encode Value as HTML

Demo Code


using System.Text;
using System.IO;//  w w  w.j av a  2  s.c  o  m
using System.Globalization;
using System;

public class Main{
        public static string EncodeValue(string value)
        {
            StringBuilder builder = new StringBuilder();
            StringReader reader = new StringReader(value);
            for (int i = reader.Read(); i != -1; i = reader.Read())
            {
                switch (i)
                {
                    case 0x20:
                        {
                            builder.Append(" ");
                            continue;
                        }
                    case 0x22:
                        {
                            builder.Append("\"");
                            continue;
                        }
                    case 0x26:
                        {
                            builder.Append("&");
                            continue;
                        }
                    case 60:
                        {
                            builder.Append("<");
                            continue;
                        }
                    case 0x3e:
                        {
                            builder.Append(">");
                            continue;
                        }
                    case 0x80:
                        {
                            builder.Append("&euro;");
                            continue;
                        }
                    case 160:
                        {
                            builder.Append("&nbsp;");
                            continue;
                        }
                    case 0xa1:
                        {
                            builder.Append("&iexcl;");
                            continue;
                        }
                    case 0xa2:
                        {
                            builder.Append("&cent;");
                            continue;
                        }
                    case 0xa3:
                        {
                            builder.Append("&pound;");
                            continue;
                        }
                    case 0xa4:
                        {
                            builder.Append("&curren;");
                            continue;
                        }
                    case 0xa5:
                        {
                            builder.Append("&yen;");
                            continue;
                        }
                    case 0xa6:
                        {
                            builder.Append("&brvbar;");
                            continue;
                        }
                    case 0xa7:
                        {
                            builder.Append("&sect;");
                            continue;
                        }
                    case 0xa8:
                        {
                            builder.Append("&uml;");
                            continue;
                        }
                    case 0xa9:
                        {
                            builder.Append("&copy;");
                            continue;
                        }
                    case 170:
                        {
                            builder.Append("&ordf;");
                            continue;
                        }
                    case 0xab:
                        {
                            builder.Append("&laquo;");
                            continue;
                        }
                    case 0xac:
                        {
                            builder.Append("&not;");
                            continue;
                        }
                    case 0xad:
                        {
                            builder.Append("&shy;");
                            continue;
                        }
                    case 0xae:
                        {
                            builder.Append("&reg;");
                            continue;
                        }
                    case 0xaf:
                        {
                            builder.Append("&macr;");
                            continue;
                        }
                    case 0xb0:
                        {
                            builder.Append("&deg;");
                            continue;
                        }
                    case 0xb1:
                        {
                            builder.Append("&plusmn;");
                            continue;
                        }
                    case 0xb2:
                        {
                            builder.Append("&sup2;");
                            continue;
                        }
                    case 0xb3:
                        {
                            builder.Append("&sup3;");
                            continue;
                        }
                    case 180:
                        {
                            builder.Append("&acute;");
                            continue;
                        }
                    case 0xb5:
                        {
                            builder.Append("&micro;");
                            continue;
                        }
                    case 0xb6:
                        {
                            builder.Append("&para;");
                            continue;
                        }
                    case 0xb7:
                        {
                            builder.Append("&middot;");
                            continue;
                        }
                    case 0xb8:
                        {
                            builder.Append("&cedil;");
                            continue;
                        }
                    case 0xb9:
                        {
                            builder.Append("&sup1;");
                            continue;
                        }
                    case 0xba:
                        {
                            builder.Append("&ordm;");
                            continue;
                        }
                    case 0xbb:
                        {
                            builder.Append("&raquo;");
                            continue;
                        }
                    case 0xbc:
                        {
                            builder.Append("&frac14;");
                            continue;
                        }
                    case 0xbd:
                        {
                            builder.Append("&frac12;");
                            continue;
                        }
                    case 190:
                        {
                            builder.Append("&frac34;");
                            continue;
                        }
                    case 0xbf:
                        {
                            builder.Append("&iquest;");
                            continue;
                        }
                    case 0xc0:
                        {
                            builder.Append("&Agrave;");
                            continue;
                        }
                    case 0xc1:
                        {
                            builder.Append("&Aacute;");
                            continue;
                        }
                    case 0xc2:
                        {
                            builder.Append("&Acirc;");
                            continue;
                        }
                    case 0xc3:
                        {
                            builder.Append("&Atilde;");
                            continue;
                        }
                    case 0xc4:
                        {
                            builder.Append("&Auml;");
                            continue;
                        }
                    case 0xc5:
                        {
                            builder.Append("&Aring;");
                            continue;
                        }
                    case 0xc6:
                        {
                            builder.Append("&AElig;");
                            continue;
                        }
                    case 0xc7:
                        {
                            builder.Append("&Ccedil;");
                            continue;
                        }
                    case 200:
                        {
                            builder.Append("&Egrave;");
                            continue;
                        }
                    case 0xc9:
                        {
                            builder.Append("&Eacute;");
                            continue;
                        }
                    case 0xca:
                        {
                            builder.Append("&Ecirc;");
                            continue;
                        }
                    case 0xcb:
                        {
                            builder.Append("&Euml;");
                            continue;
                        }
                    case 0xcc:
                        {
                            builder.Append("&Igrave;");
                            continue;
                        }
                    case 0xcd:
                        {
                            builder.Append("&Iacute;");
                            continue;
                        }
                    case 0xce:
                        {
                            builder.Append("&Icirc;");
                            continue;
                        }
                    case 0xcf:
                        {
                            builder.Append("&Iuml;");
                            continue;
                        }
                    case 0xd0:
                        {
                            builder.Append("&ETH;");
                            continue;
                        }
                    case 0xd1:
                        {
                            builder.Append("&Ntilde;");
                            continue;
                        }
                    case 210:
                        {
                            builder.Append("&Ograve;");
                            continue;
                        }
                    case 0xd3:
                        {
                            builder.Append("&Oacute;");
                            continue;
                        }
                    case 0xd4:
                        {
                            builder.Append("&Ocirc;");
                            continue;
                        }
                    case 0xd5:
                        {
                            builder.Append("&Otilde;");
                            continue;
                        }
                    case 0xd6:
                        {
                            builder.Append("&Ouml;");
                            continue;
                        }
                    case 0xd7:
                        {
                            builder.Append("&times;");
                            continue;
                        }
                    case 0xd8:
                        {
                            builder.Append("&Oslash;");
                            continue;
                        }
                    case 0xd9:
                        {
                            builder.Append("&Ugrave;");
                            continue;
                        }
                    case 0xda:
                        {
                            builder.Append("&Uacute;");
                            continue;
                        }
                    case 0xdb:
                        {
                            builder.Append("&Ucirc;");
                            continue;
                        }
                    case 220:
                        {
                            builder.Append("&Uuml;");
                            continue;
                        }
                    case 0xdd:
                        {
                            builder.Append("&Yacute;");
                            continue;
                        }
                    case 0xde:
                        {
                            builder.Append("&THORN;");
                            continue;
                        }
                    case 0xdf:
                        {
                            builder.Append("&szlig;");
                            continue;
                        }
                    case 0xe0:
                        {
                            builder.Append("&agrave;");
                            continue;
                        }
                    case 0xe1:
                        {
                            builder.Append("&aacute;");
                            continue;
                        }
                    case 0xe2:
                        {
                            builder.Append("&acirc;");
                            continue;
                        }
                    case 0xe3:
                        {
                            builder.Append("&atilde;");
                            continue;
                        }
                    case 0xe4:
                        {
                            builder.Append("&auml;");
                            continue;
                        }
                    case 0xe5:
                        {
                            builder.Append("&aring;");
                            continue;
                        }
                    case 230:
                        {
                            builder.Append("&aelig;");
                            continue;
                        }
                    case 0xe7:
                        {
                            builder.Append("&ccedil;");
                            continue;
                        }
                    case 0xe8:
                        {
                            builder.Append("&egrave;");
                            continue;
                        }
                    case 0xe9:
                        {
                            builder.Append("&eacute;");
                            continue;
                        }
                    case 0xea:
                        {
                            builder.Append("&ecirc;");
                            continue;
                        }
                    case 0xeb:
                        {
                            builder.Append("&euml;");
                            continue;
                        }
                    case 0xec:
                        {
                            builder.Append("&igrave;");
                            continue;
                        }
                    case 0xed:
                        {
                            builder.Append("&iacute;");
                            continue;
                        }
                    case 0xee:
                        {
                            builder.Append("&icirc;");
                            continue;
                        }
                    case 0xef:
                        {
                            builder.Append("&iuml;");
                            continue;
                        }
                    case 240:
                        {
                            builder.Append("&eth;");
                            continue;
                        }
                    case 0xf1:
                        {
                            builder.Append("&ntilde;");
                            continue;
                        }
                    case 0xf2:
                        {
                            builder.Append("&ograve;");
                            continue;
                        }
                    case 0xf3:
                        {
                            builder.Append("&oacute;");
                            continue;
                        }
                    case 0xf4:
                        {
                            builder.Append("&ocirc;");
                            continue;
                        }
                    case 0xf5:
                        {
                            builder.Append("&otilde;");
                            continue;
                        }
                    case 0xf6:
                        {
                            builder.Append("&ouml;");
                            continue;
                        }
                    case 0xf7:
                        {
                            builder.Append("&divide;");
                            continue;
                        }
                    case 0xf8:
                        {
                            builder.Append("&oslash;");
                            continue;
                        }
                    case 0xf9:
                        {
                            builder.Append("&ugrave;");
                            continue;
                        }
                    case 250:
                        {
                            builder.Append("&uacute;");
                            continue;
                        }
                    case 0xfb:
                        {
                            builder.Append("&ucirc;");
                            continue;
                        }
                    case 0xfc:
                        {
                            builder.Append("&uuml;");
                            continue;
                        }
                    case 0xfd:
                        {
                            builder.Append("&yacute;");
                            continue;
                        }
                    case 0xfe:
                        {
                            builder.Append("&thorn;");
                            continue;
                        }
                    case 0xff:
                        {
                            builder.Append("&yuml;");
                            continue;
                        }
                    case 0x152:
                        {
                            builder.Append("&OElig;");
                            continue;
                        }
                    case 0x153:
                        {
                            builder.Append("&oelig;");
                            continue;
                        }
                    case 0x160:
                        {
                            builder.Append("&Scaron;");
                            continue;
                        }
                    case 0x161:
                        {
                            builder.Append("&scaron;");
                            continue;
                        }
                    case 0x178:
                        {
                            builder.Append("&Yuml;");
                            continue;
                        }
                    case 0x192:
                        {
                            builder.Append("&fnof;");
                            continue;
                        }
                    case 0x391:
                        {
                            builder.Append("&Alpha;");
                            continue;
                        }
                    case 0x392:
                        {
                            builder.Append("&Beta;");
                            continue;
                        }
                    case 0x393:
                        {
                            builder.Append("&Gamma;");
                            continue;
                        }
                    case 0x394:
                        {
                            builder.Append("&Delta;");
                            continue;
                        }
                    case 0x395:
                        {
                            builder.Append("&Epsilon;");
                            continue;
                        }
                    case 0x396:
                        {
                            builder.Append("&Zeta;");
                            continue;
                        }
                    case 0x397:
                        {
                            builder.Append("&Eta;");
                            continue;
                        }
                    case 920:
                        {
                            builder.Append("&Theta;");
                            continue;
                        }
                    case 0x399:
                        {
                            builder.Append("&Iota;");
                            continue;
                        }
                    case 0x39a:
                        {
                            builder.Append("&Kappa;");
                            continue;
                        }
                    case 0x39b:
                        {
                            builder.Append("&Lambda;");
                            continue;
                        }
                    case 0x39c:
                        {
                            builder.Append("&Mu;");
                            continue;
                        }
                    case 0x39d:
                        {
                            builder.Append("&Nu;");
                            continue;
                        }
                    case 0x39e:
                        {
                            builder.Append("&Xi;");
                            continue;
                        }
                    case 0x39f:
                        {
                            builder.Append("&Omicron;");
                            continue;
                        }
                    case 0x3a0:
                        {
                            builder.Append("&Pi;");
                            continue;
                        }
                    case 0x3a1:
                        {
                            builder.Append("&Rho;");
                            continue;
                        }
                    case 0x3a3:
                        {
                            builder.Append("&Sigma;");
                            continue;
                        }
                    case 0x3a4:
                        {
                            builder.Append("&Tau;");
                            continue;
                        }
                    case 0x3a5:
                        {
                            builder.Append("&Upsilon;");
                            continue;
                        }
                    case 0x3a6:
                        {
                            builder.Append("&Phi;");
                            continue;
                        }
                    case 0x3a7:
                        {
                            builder.Append("&Chi;");
                            continue;
                        }
                    case 0x3a8:
                        {
                            builder.Append("&Psi;");
                            continue;
                        }
                    case 0x3a9:
                        {
                            builder.Append("&Omega;");
                            continue;
                        }
                    case 0x3b1:
                        {
                            builder.Append("&alpha;");
                            continue;
                        }
                    case 0x3b2:
                        {
                            builder.Append("&beta;");
                            continue;
                        }
                    case 0x3b3:
                        {
                            builder.Append("&gamma;");
                            continue;
                        }
                    case 0x3b4:
                        {
                            builder.Append("&delta;");
                            continue;
                        }
                    case 0x3b5:
                        {
                            builder.Append("&epsilon;");
                            continue;
                        }
                    case 950:
                        {
                            builder.Append("&zeta;");
                            continue;
                        }
                    case 0x3b7:
                        {
                            builder.Append("&eta;");
                            continue;
                        }
                    case 0x3b8:
                        {
                            builder.Append("&theta;");
                            continue;
                        }
                    case 0x3b9:
                        {
                            builder.Append("&iota;");
                            continue;
                        }
                    case 0x3ba:
                        {
                            builder.Append("&kappa;");
                            continue;
                        }
                    case 0x3bb:
                        {
                            builder.Append("&lambda;");
                            continue;
                        }
                    case 0x3bc:
                        {
                            builder.Append("&mu;");
                            continue;
                        }
                    case 0x3bd:
                        {
                            builder.Append("&nu;");
                            continue;
                        }
                    case 0x3be:
                        {
                            builder.Append("&xi;");
                            continue;
                        }
                    case 0x3bf:
                        {
                            builder.Append("&omicron;");
                            continue;
                        }
                    case 960:
                        {
                            builder.Append("&pi;");
                            continue;
                        }
                    case 0x3c1:
                        {
                            builder.Append("&rho;");
                            continue;
                        }
                    case 0x3c2:
                        {
                            builder.Append("&sigmaf;");
                            continue;
                        }
                    case 0x3c3:
                        {
                            builder.Append("&sigma;");
                            continue;
                        }
                    case 0x3c4:
                        {
                            builder.Append("&tau;");
                            continue;
                        }
                    case 0x3c5:
                        {
                            builder.Append("&upsilon;");
                            continue;
                        }
                    case 0x3c6:
                        {
                            builder.Append("&phi;");
                            continue;
                        }
                    case 0x3c7:
                        {
                            builder.Append("&chi;");
                            continue;
                        }
                    case 0x3c8:
                        {
                            builder.Append("&psi;");
                            continue;
                        }
                    case 0x3c9:
                        {
                            builder.Append("&omega;");
                            continue;
                        }
                    case 0x3d1:
                        {
                            builder.Append("&thetasym;");
                            continue;
                        }
                    case 0x3d2:
                        {
                            builder.Append("&upsih;");
                            continue;
                        }
                    case 0x3d6:
                        {
                            builder.Append("&piv;");
                            continue;
                        }
                    case 0x2dc:
                        {
                            builder.Append("&tilde;");
                            continue;
                        }
                    case 710:
                        {
                            builder.Append("&circ;");
                            continue;
                        }
                    case 0x2003:
                        {
                            builder.Append("&emsp;");
                            continue;
                        }
                    case 0x2009:
                        {
                            builder.Append("&thinsp;");
                            continue;
                        }
                    case 0x200c:
                        {
                            builder.Append("&zwnj;");
                            continue;
                        }
                    case 0x200d:
                        {
                            builder.Append("&zwj;");
                            continue;
                        }
                    case 0x200e:
                        {
                            builder.Append("&lrm;");
                            continue;
                        }
                    case 0x200f:
                        {
                            builder.Append("&rlm;");
                            continue;
                        }
                    case 0x2013:
                        {
                            builder.Append("&ndash;");
                            continue;
                        }
                    case 0x2014:
                        {
                            builder.Append("&mdash;");
                            continue;
                        }
                    case 0x2018:
                        {
                            builder.Append("&lsquo;");
                            continue;
                        }
                    case 0x2019:
                        {
                            builder.Append("&rsquo;");
                            continue;
                        }
                    case 0x201a:
                        {
                            builder.Append("&sbquo;");
                            continue;
                        }
                    case 0x201c:
                        {
                            builder.Append("&ldquo;");
                            continue;
                        }
                    case 0x201d:
                        {
                            builder.Append("&rdquo;");
                            continue;
                        }
                    case 0x201e:
                        {
                            builder.Append("&bdquo;");
                            continue;
                        }
                    case 0x2020:
                        {
                            builder.Append("&dagger;");
                            continue;
                        }
                    case 0x2021:
                        {
                            builder.Append("&Dagger;");
                            continue;
                        }
                    case 0x2022:
                        {
                            builder.Append("&bull;");
                            continue;
                        }
                    case 0x2026:
                        {
                            builder.Append("&hellip;");
                            continue;
                        }
                    case 0x2030:
                        {
                            builder.Append("&permil;");
                            continue;
                        }
                    case 0x2032:
                        {
                            builder.Append("&prime;");
                            continue;
                        }
                    case 0x2033:
                        {
                            builder.Append("&Prime;");
                            continue;
                        }
                    case 0x2039:
                        {
                            builder.Append("&lsaquo;");
                            continue;
                        }
                    case 0x203a:
                        {
                            builder.Append("&rsaquo;");
                            continue;
                        }
                    case 0x203e:
                        {
                            builder.Append("&oline;");
                            continue;
                        }
                    case 0x2044:
                        {
                            builder.Append("&fras1;");
                            continue;
                        }
                    case 0x2111:
                        {
                            builder.Append("&image;");
                            continue;
                        }
                    case 0x2118:
                        {
                            builder.Append("&weierp;");
                            continue;
                        }
                    case 0x211c:
                        {
                            builder.Append("&real;");
                            continue;
                        }
                    case 0x2122:
                        {
                            builder.Append("&trade;");
                            continue;
                        }
                    case 0x2190:
                        {
                            builder.Append("&larr;");
                            continue;
                        }
                    case 0x2191:
                        {
                            builder.Append("&uarr;");
                            continue;
                        }
                    case 0x2192:
                        {
                            builder.Append("&rarr;");
                            continue;
                        }
                    case 0x2193:
                        {
                            builder.Append("&darr;");
                            continue;
                        }
                    case 0x2194:
                        {
                            builder.Append("&harr;");
                            continue;
                        }
                    case 0x2135:
                        {
                            builder.Append("&alefsym;");
                            continue;
                        }
                    case 0x21d0:
                        {
                            builder.Append("&lArr;");
                            continue;
                        }
                    case 0x21d1:
                        {
                            builder.Append("&uArr;");
                            continue;
                        }
                    case 0x21d2:
                        {
                            builder.Append("&rArr;");
                            continue;
                        }
                    case 0x21d3:
                        {
                            builder.Append("&dArr;");
                            continue;
                        }
                    case 0x21d4:
                        {
                            builder.Append("&hArr;");
                            continue;
                        }
                    case 0x21b5:
                        {
                            builder.Append("&crarr;");
                            continue;
                        }
                    case 0x2200:
                        {
                            builder.Append("&forall;");
                            continue;
                        }
                    case 0x2202:
                        {
                            builder.Append("&part;");
                            continue;
                        }
                    case 0x2203:
                        {
                            builder.Append("&exist;");
                            continue;
                        }
                    case 0x2205:
                        {
                            builder.Append("&empty;");
                            continue;
                        }
                    case 0x2207:
                        {
                            builder.Append("&nabla;");
                            continue;
                        }
                    case 0x2208:
                        {
                            builder.Append("&isin;");
                            continue;
                        }
                    case 0x2209:
                        {
                            builder.Append("&notin;");
                            continue;
                        }
                    case 0x220b:
                        {
                            builder.Append("&ni;");
                            continue;
                        }
                    case 0x220f:
                        {
                            builder.Append("&prod;");
                            continue;
                        }
                    case 0x2211:
                        {
                            builder.Append("&sum;");
                            continue;
                        }
                    case 0x2212:
                        {
                            builder.Append("&minus;");
                            continue;
                        }
                    case 0x221a:
                        {
                            builder.Append("&radic;");
                            continue;
                        }
                    case 0x221d:
                        {
                            builder.Append("&prop;");
                            continue;
                        }
                    case 0x221e:
                        {
                            builder.Append("&infin;");
                            continue;
                        }
                    case 0x2220:
                        {
                            builder.Append("&ang;");
                            continue;
                        }
                    case 0x2217:
                        {
                            builder.Append("&lowast;");
                            continue;
                        }
                    case 0x2227:
                        {
                            builder.Append("&and;");
                            continue;
                        }
                    case 0x2228:
                        {
                            builder.Append("&or;");
                            continue;
                        }
                    case 0x2229:
                        {
                            builder.Append("&cap;");
                            continue;
                        }
                    case 0x222a:
                        {
                            builder.Append("&cup;");
                            continue;
                        }
                    case 0x222b:
                        {
                            builder.Append("&int;");
                            continue;
                        }
                    case 0x2234:
                        {
                            builder.Append("&there4;");
                            continue;
                        }
                    case 0x223c:
                        {
                            builder.Append("&sim;");
                            continue;
                        }
                    case 0x2245:
                        {
                            builder.Append("&cong;");
                            continue;
                        }
                    case 0x2248:
                        {
                            builder.Append("&asymp;");
                            continue;
                        }
                    case 0x2260:
                        {
                            builder.Append("&ne;");
                            continue;
                        }
                    case 0x2261:
                        {
                            builder.Append("&equiv;");
                            continue;
                        }
                    case 0x2264:
                        {
                            builder.Append("&le;");
                            continue;
                        }
                    case 0x2265:
                        {
                            builder.Append("&ge;");
                            continue;
                        }
                    case 0x2282:
                        {
                            builder.Append("&sub;");
                            continue;
                        }
                    case 0x2283:
                        {
                            builder.Append("&sup;");
                            continue;
                        }
                    case 0x2284:
                        {
                            builder.Append("&nsub;");
                            continue;
                        }
                    case 0x2286:
                        {
                            builder.Append("&sube;");
                            continue;
                        }
                    case 0x2287:
                        {
                            builder.Append("&supe;");
                            continue;
                        }
                    case 0x2295:
                        {
                            builder.Append("&oplus;");
                            continue;
                        }
                    case 0x2297:
                        {
                            builder.Append("&otimes;");
                            continue;
                        }
                    case 0x2308:
                        {
                            builder.Append("&lceil;");
                            continue;
                        }
                    case 0x2309:
                        {
                            builder.Append("&rceil;");
                            continue;
                        }
                    case 0x230a:
                        {
                            builder.Append("&lfloor;");
                            continue;
                        }
                    case 0x230b:
                        {
                            builder.Append("&rfloor;");
                            continue;
                        }
                    case 0x22c5:
                        {
                            builder.Append("&sdot;");
                            continue;
                        }
                    case 0x22a5:
                        {
                            builder.Append("&perp;");
                            continue;
                        }
                    case 0x2329:
                        {
                            builder.Append("&lang;");
                            continue;
                        }
                    case 0x232a:
                        {
                            builder.Append("&rang;");
                            continue;
                        }
                    case 0x25ca:
                        {
                            builder.Append("&loz;");
                            continue;
                        }
                    case 0x2660:
                        {
                            builder.Append("&spades;");
                            continue;
                        }
                    case 0x2663:
                        {
                            builder.Append("&clubs;");
                            continue;
                        }
                    case 0x2665:
                        {
                            builder.Append("&hearts;");
                            continue;
                        }
                    case 0x2666:
                        {
                            builder.Append("&diams;");
                            continue;
                        }
                }
                if (i <= 0x7f)
                {
                    builder.Append((char)i);
                }
                else
                {
                    builder.Append("&#" + i + ";");
                }
            }
            return builder.ToString();
        }
}

Related Tutorials