如何在App.Config中编写URI字符串 [英] How to write an URI string in App.Config

查看:173
本文介绍了如何在App.Config中编写URI字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作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的转义要求(例如,&应该为&amp;<应该为&lt;,而>应该为&gt;)./p>

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

<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 >>>"的字符串,请执行以下操作:

<appSettings>
    <add
        key="someString"
        value="I &lt;3 angle bra&lt;kets &amp; ampersands &gt;&gt;&gt;"
    />
</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 &amp;, < should be &lt; and > should be &gt;).

In your case, it should look like this:

<appSettings>
    <add
        key="fooUriString" 
        value="https://foo.bar.baz/download/DownloadStream?id=5486cfb8c50c9f9a2c1bc43daf7ddeed&amp;login=null&amp;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 &lt;3 angle bra&lt;kets &amp; ampersands &gt;&gt;&gt;"
    />
</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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆