C# 在App.Config中使用哪种编码?

C# 在App.Config中使用哪种编码?,c#,encoding,configuration-files,C#,Encoding,Configuration Files,以下config文件导致错误,因为'url'值中存在&: <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key ="url" value ="http://www.example.com/?user=admin&password=1234"/> </appSettings> </configuration

以下
config
文件导致错误,因为'url'值中存在
&

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key ="url" value ="http://www.example.com/?user=admin&password=1234"/>
  </appSettings>
</configuration>


问题是
config
文件中的
key/value
使用哪种编码?(即Url编码…

这是一个XML文件。因此,必须对所有保留字符进行XML转义:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key ="url" value ="http://www.example.com/?user=admin&amp;password=1234"/>
  </appSettings>
</configuration>


通过编程,您可以使用
System.Web.HttpUtility.HtmlEncode
方法执行这种类型的转义。是的,它的名字暗示了特定于HTML的编码,但根据我的经验,这相当于XML转义。

在.NET中哪个类有XML转义实用程序?VisualStudio编辑器不会自动替换保留字符。@Xagron,请回答“增强”以包含您的问题。