解码所有 HTML 实体
我正在寻找一些可以解码大量 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么也许您需要HttpUtility.HtmlDecode?。
它应该可以工作,您只需要添加对 System.Web 的引用。
至少在 .Net Framework 中是这样的4.
例如以下代码:
工作正常,输出符合预期(与号和版权符号)。
您确定问题出在 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:
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.
使用 .Net 4 中包含的
WebUtility.HtmlDecode
例如,如果我在控制台应用程序中运行:
我会得到
, &, c
Use
WebUtility.HtmlDecode
included in .Net 4For example, if I run in a console app:
I get
, &, c