如何在App.Config中编写URI字符串 [英] How to write an URI string in App.Config
问题描述
我正在制作Windows Service
. Service
每天晚上都必须卸载某些内容,因此我想将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
您尚未在URI中正确编码与号.请记住,app.config
是XML文件,因此您必须符合XML的转义要求(例如,&
应该为&
,<
应该为<
,而>
应该为>
)./p>
在您的情况下,它应如下所示:
<appSettings>
<add
key="fooUriString"
value="https://foo.bar.baz/download/DownloadStream?id=5486cfb8c50c9f9a2c1bc43daf7ddeed&login=null&password=null"
/>
</appSettings>
但是通常,如果您想存储一个类似于"I <3 angle bra<kets & ampersands >>>"
的字符串,请执行以下操作:
<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 );
}
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屋!