Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an URI string in App.Config

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 
like image 433
radbyx Avatar asked Oct 02 '12 07:10

radbyx


People also ask

How read app config file in C# Windows application?

Add the App. config file to your project. After creating a . NET Framework project, right-click on your project in Solution Explorer and choose Add > New Item. Choose the Application Configuration File item and then select Add.

How read and write config file in C#?

The easiest way to read/write AppSettings config file there is a special section in reserved which allows you to do exactly that. Simply add an <appsettings> section and add your data as key/value pairs of the form <add key="xxx" value="xxxx" /> . That's all to create a new app. config file with settings in it.


2 Answers

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 ); } 
like image 159
Dai Avatar answered Sep 22 '22 13:09

Dai


&amp; should work just fine, Wikipedia has a List of predefined entities in XML.

like image 21
Raab Avatar answered Sep 21 '22 13:09

Raab