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

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

问题描述

我正在制作一个 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 的转义要求(例如 & 应该是 &amp;, < 应该是 &lt; 并且 > 应该是 &gt;).

在你的情况下,它应该是这样的:

<添加键=fooUriString"value="https://foo.bar.baz/download/DownloadStream?id=5486cfb8c50c9f9a2c1bc43daf7ddeed&amp;login=null&amp;password=null"/></appSettings>

但一般来说,如果您想存储一个看起来像 "I <3 angle bra 的字符串,请执行以下操作:

<添加键=someString"value="I &lt;3 角胸罩&lt;kets &amp; &gt;&gt;>"/></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 &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天全站免登陆