如何在App.Config中编写URI字符串

时间:2012-10-02 07:07:01

标签: c# configuration windows-services uri app-config

我正在制作Windows ServiceService每晚必须下载一些东西,因此我想将URI放在App.Config中以防我以后需要更改它。

我想在App.Config中编写一个URI。什么使它无效,我应该如何处理?

<appSettings>
    <add key="fooUriString" 
         value="https://foo.bar.baz/download/DownloadStream?id=5486cfb8c50c9f9a2c1bc43daf7ddeed&login=null&password=null"/>
</appSettings>

我的错误:

- Entity 'login' not defined
- Expecting ';'
- Entity 'password' not defined
- Application Configuration file "App.config" is invalid. An error occurred

3 个答案:

答案 0 :(得分:61)

您尚未正确编码URI中的&符号。请记住,app.config是一个XML文件,因此您必须符合XML对转义的要求(例如&应为&amp;<应为&lt;和{ {1}}应为>)。

在您的情况下,它应该如下所示:

&gt;

但一般来说,如果你想存储一个看起来像<appSettings> <add key="fooUriString" value="https://foo.bar.baz/download/DownloadStream?id=5486cfb8c50c9f9a2c1bc43daf7ddeed&amp;login=null&amp;password=null" /> </appSettings> 的字符串,那就这样做:

"I <3 angle bra<kets & ampersands >>>"

答案 1 :(得分:7)

尝试使用:&amp;代替网址中的&amp;

答案 2 :(得分:7)

&amp;应该可以正常使用,维基百科有List of predefined entities in XML