如何在 App.Config 中编写 URI 字符串 [英] How to write an URI string in App.Config
问题描述
我正在制作一个 Windows 服务
.Service
必须每晚都下载一些东西,因此我想将 URI 放在 App.Config 中,以防我以后需要更改它.
我想在我的 App.Config 中编写一个 URI.是什么让它无效,我应该如何处理?
<add key="fooUriString"value="https://foo.bar.baz/download/DownloadStream?id=5486cfb8c50c9f9a2c1bc43daf7ddeed&login=null&password=null"/></appSettings>
我的错误:
- 实体登录"未定义- 期待 ';'- 实体密码"未定义- 应用程序配置文件App.config"无效.发生错误
您没有正确编码 URI 中的 & 符号.记住 app.config
是一个 XML 文件,所以你必须符合 XML 的转义要求(例如 &
应该是 &
, <
应该是 <
并且 >
应该是 >
).>
在你的情况下,它应该是这样的:
<添加键=fooUriString"value="https://foo.bar.baz/download/DownloadStream?id=5486cfb8c50c9f9a2c1bc43daf7ddeed&login=null&password=null"/></appSettings>
但一般来说,如果您想存储一个看起来像 "I <3 angle bra
<添加键=someString"value="I <3 角胸罩<kets & >>>"/></appSettings>void StringEncodingTest() {String expected = "I <3 角胸罩<kets & >>>";String actual = ConfigurationManager.AppSettings["someString"];Debug.Assert.AreEqual(预期,实际);}
I am making a Windows Service
. The Service
has to donwload something every night, and therefor I want to place the URI in the App.Config in case I later need to change it.
I want to write an URI in my App.Config. What makes it invalid and how should i approach this?
<appSettings>
<add key="fooUriString"
value="https://foo.bar.baz/download/DownloadStream?id=5486cfb8c50c9f9a2c1bc43daf7ddeed&login=null&password=null"/>
</appSettings>
My errors:
- Entity 'login' not defined
- Expecting ';'
- Entity 'password' not defined
- Application Configuration file "App.config" is invalid. An error occurred
You haven't properly encoded the ampersands in your URI. Remember that app.config
is an XML file, so you must conform to XML's requirements for escaping (e.g. &
should be &
, <
should be <
and >
should be >
).
In your case, it should look like this:
<appSettings>
<add
key="fooUriString"
value="https://foo.bar.baz/download/DownloadStream?id=5486cfb8c50c9f9a2c1bc43daf7ddeed&login=null&password=null"
/>
</appSettings>
But in general, if you wanted to store a string that looked like "I <3 angle bra<kets & ampersands >>>"
then do this:
<appSettings>
<add
key="someString"
value="I <3 angle bra<kets & ampersands >>>"
/>
</appSettings>
void StringEncodingTest() {
String expected = "I <3 angle bra<kets & ampersands >>>";
String actual = ConfigurationManager.AppSettings["someString"];
Debug.Assert.AreEqual( expected, actual );
}
这篇关于如何在 App.Config 中编写 URI 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!