解码所有 HTML 实体

发布于 2024-12-19 03:15:35 字数 223 浏览 2 评论 0原文

我正在寻找一些可以解码大量 HTML 实体的函数。

原因是我正在编写一些代码来获取 HTML 内容并将其转换为纯文本,我遇到的问题是很多实体无法使用 HttpUtility.HtmlDecode

我关心的实体的一些示例是  、&、©。

这是针对 .net 3.5 的。

I'm looking for some function that will decode a good amount of HTML entities.

Reason is I am working on some code to take HTML content and turning it into plain text, the issue that I have is a lot of entities do not get converted using HttpUtility.HtmlDecode.

Some examples of entities I'm concerned about are  , &, ©.

This is for .net 3.5.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

活泼老夫 2024-12-26 03:15:35

那么也许您需要HttpUtility.HtmlDecode?。
它应该可以工作,您只需要添加对 System.Web 的引用。
至少在 .Net Framework 中是这样的4.

例如以下代码:

MessageBox.Show(HttpUtility.HtmlDecode("&©"));

工作正常,输出符合预期(与号和版权符号)。
您确定问题出在 HtmlDecode 中而不是其他地方吗?

更新:另一个能够完成这项工作的类,WebUtility(又是 HtmlDecode 方法)出现在较新版本的 .Net 中。然而,它似乎存在一些问题。请参阅 HttpUtility 与 WebUtility 问题。

Then maybe you will need the HttpUtility.HtmlDecode?.
It should work, you just need to add a reference to System.Web.
At least this was the way in .Net Framework < 4.

For example the following code:

MessageBox.Show(HttpUtility.HtmlDecode("&©"));

Worked and the output was as expected (ampersand and copyright symbol).
Are you sure the problem is within HtmlDecode and not something else?

UPDATE: Another class capable of doing the job, WebUtility (again HtmlDecode method) came in the newer versions of .Net. However, there seem to be some problems with it. See the HttpUtility vs. WebUtility question.

如痴如狂 2024-12-26 03:15:35

使用 .Net 4 中包含的 WebUtility.HtmlDecode

例如,如果我在控制台应用程序中运行:

  Console.WriteLine(WebUtility.HtmlDecode(" , &, ©"));

我会得到 , &, c

Use WebUtility.HtmlDecode included in .Net 4

For example, if I run in a console app:

  Console.WriteLine(WebUtility.HtmlDecode(" , &, ©"));

I get , &, c

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文