没有 System.Web 的 C# HTMLDecode 可能吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/711672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# HTMLDecode without System.Web possible?
提问by miccet
I know there are different methods in the System.Web namespace for decoding html entities (such as "%20" for space). I'm building a Winforms application however, but needs to deal with html encoded strings. Basically I have the iTunes Library XML file and need to decode the URLs in there to check the files.
我知道 System.Web 命名空间中有不同的方法用于解码 html 实体(例如空格的“%20”)。然而,我正在构建一个 Winforms 应用程序,但需要处理 html 编码的字符串。基本上,我有 iTunes 库 XML 文件,需要解码其中的 URL 以检查文件。
Is this possible without the System.Web namespace?
如果没有 System.Web 命名空间,这可能吗?
采纳答案by bizon
Developers who need to use System.Web.HttpUtility in their client apps and had to reference System.Web.dll and therefore target NET4 full (System.Web.dll is in Full) , can now target the NET4 Client Profile by using the new System.Net.WebUtility class which is in System.dll (System.dll is in NET4 Client Profile). System.Net.WebUtility includes HtmlEncode and HtmlDecode. Url encoding can be accomplished using the System.Uri class (also in System.dll).
需要在其客户端应用程序中使用 System.Web.HttpUtility 并且必须引用 System.Web.dll 并因此以 NET4 full (System.Web.dll is in Full) 为目标的开发人员,现在可以使用新的System.Net.WebUtility 类位于 System.dll 中(System.dll 位于 NET4 客户端配置文件中)。System.Net.WebUtility 包括 HtmlEncode 和 HtmlDecode。Url 编码可以使用 System.Uri 类(也在 System.dll 中)完成。
回答by Jon Skeet
Just because you're writing a Windows Forms app doesn't stop you from using System.Web
. Just add a reference to System.Web.dll
.
仅仅因为您正在编写 Windows 窗体应用程序并不会阻止您使用System.Web
. 只需添加对System.Web.dll
.
回答by Mitchel Sellers
To use the methods that are in the .NET framework you must use the System.Web namespace to get the HtmlDecode method.
要使用 .NET 框架中的方法,您必须使用 System.Web 命名空间来获取 HtmlDecode 方法。
Yes, you could write your own method to do it, but that wouldn't make a lot of sense.
是的,您可以编写自己的方法来做到这一点,但这没有多大意义。
Just add the reference to system.web.
只需添加对 system.web 的引用。
回答by Steve Eisner
See this articleif you're still curious how to do this without System.Web. It offers a solution for URI decoding (which is really what you're decoding, not HTML entities which are something like "&emdash;" )
如果您仍然好奇如何在没有 System.Web 的情况下执行此操作,请参阅本文。它提供了 URI 解码的解决方案(这实际上是您要解码的内容,而不是类似于 "&emdash;" 的 HTML 实体)
回答by pathachiever11
You can use HttpUtility.UrlDecode
or HttpUtility.HtmlDecode
您可以使用HttpUtility.UrlDecode
或HttpUtility.HtmlDecode
回答by kirankumar
You can use System.Net.WebUtility.HtmlDecode
:
您可以使用System.Net.WebUtility.HtmlDecode
:
Converts a string that has been HTML-encoded for HTTP transmission into a decoded string.
将已为 HTTP 传输进行 HTML 编码的字符串转换为已解码的字符串。
回答by Simko
mybe it can help in winforms :
mybe 它可以帮助 winforms :
System.Uri.EscapeUriString(plainText);