Greetings, I'm looking for a way to encode a string into HTML that uses human-readable tags such as ê
(=ê). At the moment, I am using the HttpUtility.HtmlEncode() function, but it appears to return numbered tags instead of human-r开发者_如何学Ceadable ones. For example:
Dim str as string = HttpUtility;HtmlEncode("vente - en-tête")
'Expected: vente - en-tête
'Actually received: vente - en-tête
Is there a setting or function in ASP.Net to encode a string into HTML resembling the first comment?
EDIT: I am looking for this kind of functionality because the text is saved HTML-encoded in the database. The text comes from a bunch of MS Word documents that have been converted to HTML.
Here's a function that does the job :
public static string EntitiesEncode(string value)
{
if (string.IsNullOrEmpty(value)) return value;
var entities = new Dictionary<int, string>();
entities.Add(160, "nbsp");
entities.Add(161, "iexcl");
entities.Add(162, "cent");
entities.Add(163, "pound");
entities.Add(164, "curren");
entities.Add(165, "yen");
entities.Add(166, "brvbar");
entities.Add(167, "sect");
entities.Add(168, "uml");
entities.Add(169, "copy");
entities.Add(170, "ordf");
entities.Add(171, "laquo");
entities.Add(172, "not");
entities.Add(173, "shy");
entities.Add(174, "reg");
entities.Add(175, "macr");
entities.Add(176, "deg");
entities.Add(177, "plusmn");
entities.Add(178, "sup2");
entities.Add(179, "sup3");
entities.Add(180, "acute");
entities.Add(181, "micro");
entities.Add(182, "para");
entities.Add(183, "middot");
entities.Add(184, "cedil");
entities.Add(185, "sup1");
entities.Add(186, "ordm");
entities.Add(187, "raquo");
entities.Add(188, "frac14");
entities.Add(189, "frac12");
entities.Add(190, "frac34");
entities.Add(191, "iquest");
entities.Add(192, "Agrave");
entities.Add(193, "Aacute");
entities.Add(194, "Acirc");
entities.Add(195, "Atilde");
entities.Add(196, "Auml");
entities.Add(197, "Aring");
entities.Add(198, "AElig");
entities.Add(199, "Ccedil");
entities.Add(200, "Egrave");
entities.Add(201, "Eacute");
entities.Add(202, "Ecirc");
entities.Add(203, "Euml");
entities.Add(204, "Igrave");
entities.Add(205, "Iacute");
entities.Add(206, "Icirc");
entities.Add(207, "Iuml");
entities.Add(208, "ETH");
entities.Add(209, "Ntilde");
entities.Add(210, "Ograve");
entities.Add(211, "Oacute");
entities.Add(212, "Ocirc");
entities.Add(213, "Otilde");
entities.Add(214, "Ouml");
entities.Add(215, "times");
entities.Add(216, "Oslash");
entities.Add(217, "Ugrave");
entities.Add(218, "Uacute");
entities.Add(219, "Ucirc");
entities.Add(220, "Uuml");
entities.Add(221, "Yacute");
entities.Add(222, "THORN");
entities.Add(223, "szlig");
entities.Add(224, "agrave");
entities.Add(225, "aacute");
entities.Add(226, "acirc");
entities.Add(227, "atilde");
entities.Add(228, "auml");
entities.Add(229, "aring");
entities.Add(230, "aelig");
entities.Add(231, "ccedil");
entities.Add(232, "egrave");
entities.Add(233, "eacute");
entities.Add(234, "ecirc");
entities.Add(235, "euml");
entities.Add(236, "igrave");
entities.Add(237, "iacute");
entities.Add(238, "icirc");
entities.Add(239, "iuml");
entities.Add(240, "eth");
entities.Add(241, "ntilde");
entities.Add(242, "ograve");
entities.Add(243, "oacute");
entities.Add(244, "ocirc");
entities.Add(245, "otilde");
entities.Add(246, "ouml");
entities.Add(247, "divide");
entities.Add(248, "oslash");
entities.Add(249, "ugrave");
entities.Add(250, "uacute");
entities.Add(251, "ucirc");
entities.Add(252, "uuml");
entities.Add(253, "yacute");
entities.Add(254, "thorn");
entities.Add(255, "yuml");
entities.Add(402, "fnof");
entities.Add(913, "Alpha");
entities.Add(914, "Beta");
entities.Add(915, "Gamma");
entities.Add(916, "Delta");
entities.Add(917, "Epsilon");
entities.Add(918, "Zeta");
entities.Add(919, "Eta");
entities.Add(920, "Theta");
entities.Add(921, "Iota");
entities.Add(922, "Kappa");
entities.Add(923, "Lambda");
entities.Add(924, "Mu");
entities.Add(925, "Nu");
entities.Add(926, "Xi");
entities.Add(927, "Omicron");
entities.Add(928, "Pi");
entities.Add(929, "Rho");
entities.Add(931, "Sigma");
entities.Add(932, "Tau");
entities.Add(933, "Upsilon");
entities.Add(934, "Phi");
entities.Add(935, "Chi");
entities.Add(936, "Psi");
entities.Add(937, "Omega");
entities.Add(945, "alpha");
entities.Add(946, "beta");
entities.Add(947, "gamma");
entities.Add(948, "delta");
entities.Add(949, "epsilon");
entities.Add(950, "zeta");
entities.Add(951, "eta");
entities.Add(952, "theta");
entities.Add(953, "iota");
entities.Add(954, "kappa");
entities.Add(955, "lambda");
entities.Add(956, "mu");
entities.Add(957, "nu");
entities.Add(958, "xi");
entities.Add(959, "omicron");
entities.Add(960, "pi");
entities.Add(961, "rho");
entities.Add(962, "sigmaf");
entities.Add(963, "sigma");
entities.Add(964, "tau");
entities.Add(965, "upsilon");
entities.Add(966, "phi");
entities.Add(967, "chi");
entities.Add(968, "psi");
entities.Add(969, "omega");
entities.Add(977, "thetasym");
entities.Add(978, "upsih");
entities.Add(982, "piv");
entities.Add(8226, "bull");
entities.Add(8230, "hellip");
entities.Add(8242, "prime");
entities.Add(8243, "Prime");
entities.Add(8254, "oline");
entities.Add(8260, "frasl");
entities.Add(8472, "weierp");
entities.Add(8465, "image");
entities.Add(8476, "real");
entities.Add(8482, "trade");
entities.Add(8501, "alefsym");
entities.Add(8592, "larr");
entities.Add(8593, "uarr");
entities.Add(8594, "rarr");
entities.Add(8595, "darr");
entities.Add(8596, "harr");
entities.Add(8629, "crarr");
entities.Add(8656, "lArr");
entities.Add(8657, "uArr");
entities.Add(8658, "rArr");
entities.Add(8659, "dArr");
entities.Add(8660, "hArr");
entities.Add(8704, "forall");
entities.Add(8706, "part");
entities.Add(8707, "exist");
entities.Add(8709, "empty");
entities.Add(8711, "nabla");
entities.Add(8712, "isin");
entities.Add(8713, "notin");
entities.Add(8715, "ni");
entities.Add(8719, "prod");
entities.Add(8721, "sum");
entities.Add(8722, "minus");
entities.Add(8727, "lowast");
entities.Add(8730, "radic");
entities.Add(8733, "prop");
entities.Add(8734, "infin");
entities.Add(8736, "ang");
entities.Add(8743, "and");
entities.Add(8744, "or");
entities.Add(8745, "cap");
entities.Add(8746, "cup");
entities.Add(8747, "int");
entities.Add(8756, "there4");
entities.Add(8764, "sim");
entities.Add(8773, "cong");
entities.Add(8776, "asymp");
entities.Add(8800, "ne");
entities.Add(8801, "equiv");
entities.Add(8804, "le");
entities.Add(8805, "ge");
entities.Add(8834, "sub");
entities.Add(8835, "sup");
entities.Add(8836, "nsub");
entities.Add(8838, "sube");
entities.Add(8839, "supe");
entities.Add(8853, "oplus");
entities.Add(8855, "otimes");
entities.Add(8869, "perp");
entities.Add(8901, "sdot");
entities.Add(8968, "lceil");
entities.Add(8969, "rceil");
entities.Add(8970, "lfloor");
entities.Add(8971, "rfloor");
entities.Add(9001, "lang");
entities.Add(9002, "rang");
entities.Add(9674, "loz");
entities.Add(9824, "spades");
entities.Add(9827, "clubs");
entities.Add(9829, "hearts");
entities.Add(9830, "diams");
entities.Add(34, "quot");
entities.Add(38, "amp");
entities.Add(60, "lt");
entities.Add(62, "gt");
entities.Add(338, "OElig");
entities.Add(339, "oelig");
entities.Add(352, "Scaron");
entities.Add(353, "scaron");
entities.Add(376, "Yuml");
entities.Add(710, "circ");
entities.Add(732, "tilde");
entities.Add(8194, "ensp");
entities.Add(8195, "emsp");
entities.Add(8201, "thinsp");
entities.Add(8204, "zwnj");
entities.Add(8205, "zwj");
entities.Add(8206, "lrm");
entities.Add(8207, "rlm");
entities.Add(8211, "ndash");
entities.Add(8212, "mdash");
entities.Add(8216, "lsquo");
entities.Add(8217, "rsquo");
entities.Add(8218, "sbquo");
entities.Add(8220, "ldquo");
entities.Add(8221, "rdquo");
entities.Add(8222, "bdquo");
entities.Add(8225, "dagger");
entities.Add(8224, "Dagger");
entities.Add(8240, "permil");
entities.Add(8249, "lsaquo");
entities.Add(8250, "rsaquo");
entities.Add(8364, "euro");
StringBuilder builder = new StringBuilder();
foreach (var item in value)
{
var key = (int)item;
if (entities.ContainsKey(key))
{
builder.Append('&');
builder.Append(entities[key]);
builder.Append(';');
}
else if (key >= 160)
{
builder.Append("&#");
builder.Append(key.ToString(CultureInfo.InvariantCulture));
builder.Append(';');
}
else builder.Append(item);
}
return builder.ToString();
}
EDIT : seems you are using VB so here is the VB version :
Public Function EntitiesEncode(ByVal value As String) As String
If String.IsNullOrEmpty(value) Then
Return value
End If
Dim entities = New Dictionary(Of Integer, String)()
entities.Add(160, "nbsp")
entities.Add(161, "iexcl")
entities.Add(162, "cent")
entities.Add(163, "pound")
entities.Add(164, "curren")
entities.Add(165, "yen")
entities.Add(166, "brvbar")
entities.Add(167, "sect")
entities.Add(168, "uml")
entities.Add(169, "copy")
entities.Add(170, "ordf")
entities.Add(171, "laquo")
entities.Add(172, "not")
entities.Add(173, "shy")
entities.Add(174, "reg")
entities.Add(175, "macr")
entities.Add(176, "deg")
entities.Add(177, "plusmn")
entities.Add(178, "sup2")
entities.Add(179, "sup3")
entities.Add(180, "acute")
entities.Add(181, "micro")
entities.Add(182, "para")
entities.Add(183, "middot")
entities.Add(184, "cedil")
entities.Add(185, "sup1")
entities.Add(186, "ordm")
entities.Add(187, "raquo")
entities.Add(188, "frac14")
entities.Add(189, "frac12")
entities.Add(190, "frac34")
entities.Add(191, "iquest")
entities.Add(192, "Agrave")
entities.Add(193, "Aacute")
entities.Add(194, "Acirc")
entities.Add(195, "Atilde")
entities.Add(196, "Auml")
entities.Add(197, "Aring")
entities.Add(198, "AElig")
entities.Add(199, "Ccedil")
entities.Add(200, "Egrave")
entities.Add(201, "Eacute")
entities.Add(202, "Ecirc")
entities.Add(203, "Euml")
entities.Add(204, "Igrave")
entities.Add(205, "Iacute")
entities.Add(206, "Icirc")
entities.Add(207, "Iuml")
entities.Add(208, "ETH")
entities.Add(209, "Ntilde")
entities.Add(210, "Ograve")
entities.Add(211, "Oacute")
entities.Add(212, "Ocirc")
entities.Add(213, "Otilde")
entities.Add(214, "Ouml")
entities.Add(215, "times")
entities.Add(216, "Oslash")
entities.Add(217, "Ugrave")
entities.Add(218, "Uacute")
entities.Add(219, "Ucirc")
entities.Add(220, "Uuml")
entities.Add(221, "Yacute")
entities.Add(222, "THORN")
entities.Add(223, "szlig")
entities.Add(224, "agrave")
entities.Add(225, "aacute")
entities.Add(226, "acirc")
entities.Add(227, "atilde")
entities.Add(228, "auml")
entities.Add(229, "aring")
entities.Add(230, "aelig")
entities.Add(231, "ccedil")
entities.Add(232, "egrave")
entities.Add(233, "eacute")
entities.Add(234, "ecirc")
entities.Add(235, "euml")
entities.Add(236, "igrave")
entities.Add(237, "iacute")
entities.Add(238, "icirc")
entities.Add(239, "iuml")
entities.Add(240, "eth")
entities.Add(241, "ntilde")
entities.Add(242, "ograve")
entities.Add(243, "oacute")
entities.Add(244, "ocirc")
entities.Add(245, "otilde")
entities.Add(246, "ouml")
entities.Add(247, "divide")
entities.Add(248, "oslash")
entities.Add(249, "ugrave")
entities.Add(250, "uacute")
entities.Add(251, "ucirc")
entities.Add(252, "uuml")
entities.Add(253, "yacute")
entities.Add(254, "thorn")
entities.Add(255, "yuml")
entities.Add(402, "fnof")
entities.Add(913, "Alpha")
entities.Add(914, "Beta")
entities.Add(915, "Gamma")
entities.Add(916, "Delta")
entities.Add(917, "Epsilon")
entities.Add(918, "Zeta")
entities.Add(919, "Eta")
entities.Add(920, "Theta")
entities.Add(921, "Iota")
entities.Add(922, "Kappa")
entities.Add(923, "Lambda")
entities.Add(924, "Mu")
entities.Add(925, "Nu")
entities.Add(926, "Xi")
entities.Add(927, "Omicron")
entities.Add(928, "Pi")
entities.Add(929, "Rho")
entities.Add(931, "Sigma")
entities.Add(932, "Tau")
entities.Add(933, "Upsilon")
entities.Add(934, "Phi")
entities.Add(935, "Chi")
entities.Add(936, "Psi")
entities.Add(937, "Omega")
entities.Add(945, "alpha")
entities.Add(946, "beta")
entities.Add(947, "gamma")
entities.Add(948, "delta")
entities.Add(949, "epsilon")
entities.Add(950, "zeta")
entities.Add(951, "eta")
entities.Add(952, "theta")
entities.Add(953, "iota")
entities.Add(954, "kappa")
entities.Add(955, "lambda")
entities.Add(956, "mu")
entities.Add(957, "nu")
entities.Add(958, "xi")
entities.Add(959, "omicron")
entities.Add(960, "pi")
entities.Add(961, "rho")
entities.Add(962, "sigmaf")
entities.Add(963, "sigma")
entities.Add(964, "tau")
entities.Add(965, "upsilon")
entities.Add(966, "phi")
entities.Add(967, "chi")
entities.Add(968, "psi")
entities.Add(969, "omega")
entities.Add(977, "thetasym")
entities.Add(978, "upsih")
entities.Add(982, "piv")
entities.Add(8226, "bull")
entities.Add(8230, "hellip")
entities.Add(8242, "prime")
entities.Add(8243, "Prime")
entities.Add(8254, "oline")
entities.Add(8260, "frasl")
entities.Add(8472, "weierp")
entities.Add(8465, "image")
entities.Add(8476, "real")
entities.Add(8482, "trade")
entities.Add(8501, "alefsym")
entities.Add(8592, "larr")
entities.Add(8593, "uarr")
entities.Add(8594, "rarr")
entities.Add(8595, "darr")
entities.Add(8596, "harr")
entities.Add(8629, "crarr")
entities.Add(8656, "lArr")
entities.Add(8657, "uArr")
entities.Add(8658, "rArr")
entities.Add(8659, "dArr")
entities.Add(8660, "hArr")
entities.Add(8704, "forall")
entities.Add(8706, "part")
entities.Add(8707, "exist")
entities.Add(8709, "empty")
entities.Add(8711, "nabla")
entities.Add(8712, "isin")
entities.Add(8713, "notin")
entities.Add(8715, "ni")
entities.Add(8719, "prod")
entities.Add(8721, "sum")
entities.Add(8722, "minus")
entities.Add(8727, "lowast")
entities.Add(8730, "radic")
entities.Add(8733, "prop")
entities.Add(8734, "infin")
entities.Add(8736, "ang")
entities.Add(8743, "and")
entities.Add(8744, "or")
entities.Add(8745, "cap")
entities.Add(8746, "cup")
entities.Add(8747, "int")
entities.Add(8756, "there4")
entities.Add(8764, "sim")
entities.Add(8773, "cong")
entities.Add(8776, "asymp")
entities.Add(8800, "ne")
entities.Add(8801, "equiv")
entities.Add(8804, "le")
entities.Add(8805, "ge")
entities.Add(8834, "sub")
entities.Add(8835, "sup")
entities.Add(8836, "nsub")
entities.Add(8838, "sube")
entities.Add(8839, "supe")
entities.Add(8853, "oplus")
entities.Add(8855, "otimes")
entities.Add(8869, "perp")
entities.Add(8901, "sdot")
entities.Add(8968, "lceil")
entities.Add(8969, "rceil")
entities.Add(8970, "lfloor")
entities.Add(8971, "rfloor")
entities.Add(9001, "lang")
entities.Add(9002, "rang")
entities.Add(9674, "loz")
entities.Add(9824, "spades")
entities.Add(9827, "clubs")
entities.Add(9829, "hearts")
entities.Add(9830, "diams")
entities.Add(34, "quot")
entities.Add(38, "amp")
entities.Add(60, "lt")
entities.Add(62, "gt")
entities.Add(338, "OElig")
entities.Add(339, "oelig")
entities.Add(352, "Scaron")
entities.Add(353, "scaron")
entities.Add(376, "Yuml")
entities.Add(710, "circ")
entities.Add(732, "tilde")
entities.Add(8194, "ensp")
entities.Add(8195, "emsp")
entities.Add(8201, "thinsp")
entities.Add(8204, "zwnj")
entities.Add(8205, "zwj")
entities.Add(8206, "lrm")
entities.Add(8207, "rlm")
entities.Add(8211, "ndash")
entities.Add(8212, "mdash")
entities.Add(8216, "lsquo")
entities.Add(8217, "rsquo")
entities.Add(8218, "sbquo")
entities.Add(8220, "ldquo")
entities.Add(8221, "rdquo")
entities.Add(8222, "bdquo")
entities.Add(8225, "dagger")
entities.Add(8224, "Dagger")
entities.Add(8240, "permil")
entities.Add(8249, "lsaquo")
entities.Add(8250, "rsaquo")
entities.Add(8364, "euro")
Dim builder As New StringBuilder()
For Each item In value
Dim key = AscW(item)
If entities.ContainsKey(key) Then
builder.Append("&"c)
builder.Append(entities(key))
builder.Append(";"c)
ElseIf key >= 160 Then
builder.Append("&#")
builder.Append(key.ToString(CultureInfo.InvariantCulture))
builder.Append(";"c)
Else
builder.Append(item)
End If
Next
Return builder.ToString()
End Function
精彩评论