开发者

Which encoding to use in App.Config?

开发者 https://www.devze.com 2023-01-31 07:49 出处:网络
Following config file causes error because of & in \'url\' value: <?xml version=\"1.0\" encoding=\"utf-8\" ?>

Following config file causes error because of & in 'url' value:

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

The 开发者_C百科question is which encoding to use for key/value in a config file ? (i.e Url Encoding ...)


This is an XML file. As such, you must XML-escape all reserved characters:

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

Programmatically you can perform this type of escaping using the System.Web.HttpUtility.HtmlEncode method. Yes, its name suggests HTML specific encoding, but in my experience this is equivalent to XML escaping.

0

精彩评论

暂无评论...
验证码 换一张
取 消