我发现了关于Python和Javascript的类似问题和答案,但没有针对C#或其他WinRT兼容语言的内容。
我认为我需要它的原因是因为我正在在Windows 8商店应用程序中显示从网站获取的文本。例如,é
应该变成 é
。
还有更好的方法吗?我不是显示网站或RSS提要,而只是一个网站列表及其标题。
我发现了关于Python和Javascript的类似问题和答案,但没有针对C#或其他WinRT兼容语言的内容。
我认为我需要它的原因是因为我正在在Windows 8商店应用程序中显示从网站获取的文本。例如,é
应该变成 é
。
还有更好的方法吗?我不是显示网站或RSS提要,而只是一个网站列表及其标题。
我建议使用System.Net.WebUtility.HtmlDecode而不是HttpUtility.HtmlDecode
。
这是因为在Winforms/WPF/Console应用程序中不存在System.Web
的引用,你可以使用这个类得到完全相同的结果(它已经被添加为所有这些项目的引用)。
用法:
string s = System.Net.WebUtility.HtmlDecode("é"); // Returns é
HttpUtility.HtmlDecode
做了正确的事情。 - Raditya Kurnianto使用 HttpUtility.HtmlDecode()
方法。在 msdn 上阅读 此处 的文档。
decodedString = HttpUtility.HtmlDecode(myEncodedString)
{
这样的字符 :( - Maria Ines Parnisari这可能很有用,将所有(就我需求而言)的实体替换为它们的Unicode等效项。
public string EntityToUnicode(string html) {
var replacements = new Dictionary<string, string>();
var regex = new Regex("(&[a-z]{2,5};)");
foreach (Match match in regex.Matches(html)) {
if (!replacements.ContainsKey(match.Value)) {
var unicode = HttpUtility.HtmlDecode(match.Value);
if (unicode.Length == 1) {
replacements.Add(match.Value, string.Concat("&#", Convert.ToInt32(unicode[0]), ";"));
}
}
}
foreach (var replacement in replacements) {
html = html.Replace(replacement.Key, replacement.Value);
}
return html;
}
var regex = new Regex("(&[a-zA-Z]{2,7};)");
,以便包括诸如Ã
之类的字符。 - chrisofspades{
string inStr = "ó";
string auxStr = System.Net.WebUtility.HtmlEncode(inStr);
// auxStr == ó
string outStr = System.Net.WebUtility.HtmlDecode(auxStr);
// outStr == ó
string outStr2 = System.Net.WebUtility.HtmlDecode("ó");
// outStr2 == ó
}
{
string inStr = "ó";
string auxStr = System.Net.WebUtility.HtmlEncode(inStr);
// auxStr == ó
string outStr = System.Net.WebUtility.HtmlDecode(auxStr);
// outStr == ó
string outStr2 = System.Net.WebUtility.HtmlDecode("ó");
// outStr2 == ó
}
System.Net.WebUtility.HtmlDecode()
之前先使用HTML ISO-8859-1 Reference中的表格进行了实现。这对我很有用,可以替换常见和Unicode实体。
private static readonly Regex HtmlEntityRegex = new Regex("&(#)?([a-zA-Z0-9]*);");
public static string HtmlDecode(this string html)
{
if (html.IsNullOrEmpty()) return html;
return HtmlEntityRegex.Replace(html, x => x.Groups[1].Value == "#"
? ((char)int.Parse(x.Groups[2].Value)).ToString()
: HttpUtility.HtmlDecode(x.Groups[0].Value));
}
[Test]
[TestCase(null, null)]
[TestCase("", "")]
[TestCase("'fark'", "'fark'")]
[TestCase(""fark"", "\"fark\"")]
public void should_remove_html_entities(string html, string expected)
{
html.HtmlDecode().ShouldEqual(expected);
}
改进的Zumey方法(我无法在那里发表评论)。实体中的最大字符大小为:&exclamation(11)。实体中也可以使用大写字母,例如À(来源于wiki)。
public string EntityToUnicode(string html) {
var replacements = new Dictionary<string, string>();
var regex = new Regex("(&[a-zA-Z]{2,11};)");
foreach (Match match in regex.Matches(html)) {
if (!replacements.ContainsKey(match.Value)) {
var unicode = HttpUtility.HtmlDecode(match.Value);
if (unicode.Length == 1) {
replacements.Add(match.Value, string.Concat("&#", Convert.ToInt32(unicode[0]), ";"));
}
}
}
foreach (var replacement in replacements) {
html = html.Replace(replacement.Key, replacement.Value);
}
return html;
}