C# 转换 & 对等

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1562360/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:53:28  来源:igfitidea点击:

Converting & to & etc

c#html-encodestring-conversion

提问by

I want to convert &to &, "to " etc. Is there a function in c# that could do that without writing all the options manually?

我想转换&为 &,"为 " 等。 c# 中是否有一个函数可以在不手动编写所有选项的情况下做到这一点?

回答by Matt Hamsmith

System.Web.HttpUtility.HtmlDecode()

Edit: Note from herethat "To encode or decode values outside of a web application, use..."

编辑:请注意此处“要在 Web 应用程序之外对值进行编码或解码,请使用...”

System.Net.WebUtility.HtmlDecode()

回答by Ian Kemp

Use the static method

使用静态方法

HttpUtility.HtmlEncode

to change &to &and "to ". Use

要改变&&""。用

HttpUtility.HtmlDecode

to do the reverse.

做相反的事情。

回答by Divina

A good article about this subject : Different ways how to escape an XML string in C#

关于这个主题的一篇好文章:如何在 C# 中转义 XML 字符串的不同方法

回答by Bartosz Wójcik

For .NET < 4 simple encoder

对于 .NET < 4 简单编码器

    public static string HtmlEncode(string value)
    {
        return value.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;");
    }

回答by Corredera Romain

You can use System.Net.WebUtility.HtmlDecode(uri);

您可以使用 System.Net.WebUtility.HtmlDecode(uri);

回答by Alex W.

using System.Web; 
...
var html = "this is a sample &amp; string"; 
var decodedhtml = HttpUtility.HtmlDecode(html);