2012-11-21 55 views
24

我发现Python和Javascript类似的问题和答案,但不适用于C#或任何其他WinRT兼容语言。将HTML实体转换为C#中的Unicode字符

我认为我需要它的原因是因为我正在显示从Windows 8商店应用程序中的网站获取的文本。例如。 é应该变成é

或者还有更好的方法吗?我没有显示网站或rss提要,但只是一个网站及其标题的列表。

+0

重复: http://stackoverflow.com/questions/5783817/convert-character-entities-to-their-unicode-equivalents –

+1

其实事实并非如此。他有一个不同的问题。 – Remy

回答

42

我建议使用System.Net.WebUtility.HtmlDecodeHttpUtility.HtmlDecode

这是由于在Winforms/WPF/Console应用程序中不存在System.Web引用的事实,并且您可以使用此类(已在所有这些项目中添加为参考)获得完全相同的结果。

使用方法:在Metro应用和WP8应用HTML实体和HTML数

string s = System.Net.WebUtility.HtmlDecode("é"); // Returns é 
+0

愚蠢的我,我想那只是最简单的使用方法实体... – Remy

+4

“你可以得到完全相同的结果,使用这个类” - 错误只有HttpUtility实现将正确解码为作为WP8上的一个撇号 –

+0

在我的情况下,'HttpUtility.HtmlDecoded'做正确的事情。 –

6

使用HttpUtility.HtmlDecode() .Read MSDN上here

decodedString = HttpUtility.HtmlDecode(myEncodedString) 
+0

是的,请注意,对于WinForms或控制台应用程序,您首先必须添加对System.Web程序集的引用。 –

+0

嗨,我试过这个解决方案,但它不能解码像'{':( –

+0

@ l19这样的字符是一个公认的htmlentity?我找不到它在这个[list](http://en.wikipedia。然而,我确实设法在W3C规范中找到它,这可能就是为什么它还没有解码的原因。 – crush

3

不同的编码/编码。

随着Windows运行时Metro应用

{ 
    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 == ó 
} 

随着Windows Phone 8.0

{ 
    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 == ó 
} 

为了解决这个问题,在WP8,我已经打电话System.Net.WebUtility.HtmlDecode()之前实施的表HTML ISO-8859-1 Reference

10

这可能是有用的,用它们的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; 
    } 
+1

为我工作情况,但我编辑了正则表达式“var regex = new Regex(”(&[az] {2,6};)“);” 有很多HTML字符超过5(如$ eacute;) – forumma

+0

我也建议将正则表达式更改为'var regex = new Regex(“(&[a-zA-Z] {2,7} ;)“);'以便包括'&Atilde;'这样的字符。 – chrisofspades

0

这对我有用,取代了常用和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("&#39;fark&#39;", "'fark'")] 
[TestCase("&quot;fark&quot;", "\"fark\"")] 
public void should_remove_html_entities(string html, string expected) 
{ 
    html.HtmlDecode().ShouldEqual(expected); 
} 
相关问题