app.config problem in reading multiple values per key, very stuck no idea

P

Peted

hello, i have a app.config file like so


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="rdbUser1Cpu1" value="Cpu1,test"/>
</appSettings>
</configuration>

I understand it is possible to have mutliple values per key that when
you use getvalues is suppoded to return a String[] but it dont seem to
work

Eg

String[] temp;

temp = ConfigurationManager.AppSettings.GetValues("rdbUser1Cpu1");



this compiles and runs fine with no errors except

it does not return a string array with 2 elements houseing each
seperate value

(What i expected was temp[0] =" Cpu1" and temp[1] = "test" )

instead it returns a single element array of "Cpu1,test1" in the first
index.

temp[0] = "Cpu1,test"
temp[1] = out of bounds index

Am i misunderstanding how it should work, or am i doing this wrong.

thanks for any help

Peted
 
P

Peter Duniho

Peted said:
hello, i have a app.config file like so


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="rdbUser1Cpu1" value="Cpu1,test"/>
</appSettings>
</configuration>

I understand it is possible to have mutliple values per key that when
you use getvalues is suppoded to return a String[] but it dont seem to
work

Well, I think it's obvious that there's no reason to expect the comma
character to be somehow understood as a delimiter for a string array.

As far as the specific question goes, IMHO a better approach to dealing
with this sort of issue would be to go the other way, by calling
NameValueCollection.Add() with each element in a string array and then
looking at what the resulting XML looks like.

Now, that said, while I haven't done much myself with this area of the
API, I will bet you anything that if you simply include multiple "add"
records for a given key, that would do exactly what you want. For example:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="rdbUser1Cpu1" value="Cpu1"/>
<add key="rdbUser1Cpu1" value="test"/>
</appSettings>
</configuration>

Pete
 
P

Peted

Now, that said, while I haven't done much myself with this area of the
API, I will bet you anything that if you simply include multiple "add"
records for a given key, that would do exactly what you want. For example:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="rdbUser1Cpu1" value="Cpu1"/>
<add key="rdbUser1Cpu1" value="test"/>
</appSettings>
</configuration>

Pete


No that dont work
Ive already tried this, if you multiple add statements withe the same
key name you get this below as ive shown it


<configuration>
<appSettings>
<add key="rdbUser1Cpu1" value="Cpu1,test"/>
</appSettings>
</configuration>


multiple add statements result in comma delimited string as in my
example

this is why i do expect the comma to be the delimter for the returned
string array. From everything ive read on the net it should work the
way im expecting it to work but it does not.

Other people on the net have asked exactly the same question but i
cannot find a single instance of any explanation or an example of why
it does not work or how it can be made to work.


any advice appreciated

thanks

Peted
 
A

Alexander

Hi,

If the comma should be a delimiter, isn't it just possible to split the
string:

String[] temp =
ConfigurationManager.AppSettings.GetValues("rdbUser1Cpu1").ToString().Split(',');

- Alexander


in message news:[email protected]...
 
P

Peter Duniho

Peted said:
No that dont work
Ive already tried this, if you multiple add statements withe the same
key name you get this below as ive shown it

Okay, well it was worth a guess. :)
[...]
multiple add statements result in comma delimited string as in my
example

this is why i do expect the comma to be the delimter for the returned
string array. From everything ive read on the net it should work the
way im expecting it to work but it does not.

Can you offer some examples of what you've read on the net that indicate
it should work in the way you expect?

From the MSDN docs, I see that the NameValueCollection class supports
multiple values for a given key. I also see that the Configuration
class uses the NameValueCollection class to maintain the collection of
values. But I saw nothing in the docs to suggest that the Configuration
class necessarily persists the NameValueCollection to the XML file in
the way you (and I) expect.
Other people on the net have asked exactly the same question but i
cannot find a single instance of any explanation or an example of why
it does not work or how it can be made to work.

any advice appreciated

I have little to offer, given that I haven't used this stuff directly (I
always just go through the regular app "Properties" namespace that you
can get via Visual Studio. That said, a couple of thoughts:

As for why it might not work, see my comment above. Yes,
NameValueCollection says it will maintain multiple values for a given
key. And as near as I can tell, that's true.

But there's nothing in the Configuration class that says it does
something useful when you do add multiple values for a given key. It
seems possible to me that this was never an intended use for the
Configuration class, and that if you want to use a NameValueCollection
in the way you are trying to use it, you need to write your own custom
persistence code, so that the XML is output the way you want.

I think it's possible that all that's wrong here is that the
documentation doesn't provide enough detail on exactly what the
Configuration.Save method actually does.

Which brings me to the second thought. While Googling regarding your
question, I came across this article, called "How to make AppSettings
work with multiple values for a key":

http://www.codeproject.com/dotnet/namevaluemultiple.asp

It appears to be somewhat, if not directly, related to your question.
And in particular, they show an example of an XML file that is laid out
similar to how we'd expect it to be.

Hope that helps.

Pete
 
P

Peted

From the MSDN docs, I see that the NameValueCollection class supports
multiple values for a given key. I also see that the Configuration
class uses the NameValueCollection class to maintain the collection of
values. But I saw nothing in the docs to suggest that the Configuration
class necessarily persists the NameValueCollection to the XML file in
the way you (and I) expect.

well at least it shows im not halucinating :)

But there's nothing in the Configuration class that says it does
something useful when you do add multiple values for a given key. It
seems possible to me that this was never an intended use for the
Configuration class, and that if you want to use a NameValueCollection
in the way you are trying to use it, you need to write your own custom
persistence code, so that the XML is output the way you want.

I think it's possible that all that's wrong here is that the
documentation doesn't provide enough detail on exactly what the
Configuration.Save method actually does.

im suspecting that you are right about this

thanks

Peted
 
P

Peted

Hi,

If the comma should be a delimiter, isn't it just possible to split the
string:

String[] temp =
ConfigurationManager.AppSettings.GetValues("rdbUser1Cpu1").ToString().Split(',');

- Alexander
I dont know why i didnt think of that, i will try it

thanks

Peted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top