【问题标题】:Convert HTML entity in XML to its equivalent Unicode in C#将 XML 中的 HTML 实体转换为 C# 中的等效 Unicode
【发布时间】:2012-12-20 15:45:47
【问题描述】:

Web 服务以XmlDocument 类型返回的 XML 中有几个 HTML 实体。在应用 XSLT 转换之前,我需要将它们替换为等效的 Unicode 字符。

XML 片段

<ics>
 <record>
  <metadata>
    <meta name="Abstract" content="In the series of compounds observed after effect of &amp;#947;-quanta"/>
  </metadata>
 </record>
</ics>

我在 .Net 4.0 中使用 C#。我尝试在上述 XmlDocument 的 OuterXml 属性上使用 HttpUtility.HtmlDecode,但它不会将 HTML 实体转换为 Unicode。

如何做到这一点?

编辑:

我看到应用 HtmlDecode 一次会得到 &amp;amp;#947;&amp;#947;。如果我再次应用它,我会得到所需的 Unicode。

有更好的方法吗?

【问题讨论】:

标签: c# xml html-entities


【解决方案1】:

在 .NET 4.0 中使用 WebUtility.HtmlDecode

另外,&amp;amp;#947; 逐字解码为&amp;#947;不是 Unicode 字符γ。主要问题是您的“HTML”不正确。您必须执行两次才能获得 gamma 字符。

【讨论】:

  • 谢谢,我尝试在 OuterXml 上同时使用 WebUtility.HtmlDecode 和 HttpUtility.HtmlDecode,但生成的字符串有 γ而不是它的 Unicode 等价物。
猜你喜欢
  • 2010-09-28
  • 2019-04-17
  • 2010-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多