multiple same section in app.config

  • Thread starter Thread starter Droopy
  • Start date Start date
D

Droopy

Hi,

I would like to configure some device connections through app.config.
I created the following config file but it fails because I have the same
section more than once :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="Connections">
<section name="Connection"
type="System.Configuration.NameValueSectionHandler" />
</sectionGroup>
</configSections>
<Connections>
<Connection>
<add key="ComPort" value="COM2" />
<add key="Name" value="Connection 1" />
<add key="ID" value="1" />
</Connection>
<Connection>
<add key="ComPort" value="COM4" />
<add key="Name" value="Connection 2" />
<add key="ID" value="2" />
</Connection>
</Connections>
</configuration>

I have 2 questions :

1) How should I modify this configuration file ?
2) Now that ConfigurationSettings.GetConfig is obsolete, is does not
seems clear for me how to use ConfigurationManager to read the config
file.

Thanks in advance,

Droopy.
 
Droopy,

The reason this doesn't work is that you are trying to create something
of a heiarchical nature for data that is simply a set of pairs.

If you want to use the NameValueSectionHandler, then you need to do
something like this:

<Connections>
<add key="ComPort_1" value="COM2" />
<add key="Name_1" value="Connection 1" />
<add key="ComPort_2" value="COM4" />
<add key="Name_2" value="Connection 2" />
</Connections>

Then, you would have to parse the names to see how the values are
grouped together.

If you want to have a different kind of config setup, then you will have
to create your own custom handler.

Hope this helps.
 
I already tried your solution.
I got an exception "Sections must only appear once per config file".
 
That is a good idea but I don't really like it.
The parsing seems a bit too complicated.
I am going to try my own custom handler.

Thanks.
 
My problem is that I tried to use the same section multiple times.
A custom section won't help I suppose ?
 
Droopy,

Well, it will, but you have to have one root element that is not
repeated.
 
Droopy,

Well, it will, but you have to have one root element that is not
repeated.

So I must miss something !

I tried with the example provided in MSDN :

<myCustomGroup>
<myCustomSection myAttrib1="Clowns">
<myChildSection
myChildAttrib1="Zippy"
myChildAttrib2="Michael Zawondy "/>
</myCustomSection>
</myCustomGroup>

It works of course.
When I try to add a 2nd element :

<myCustomGroup>
<myCustomSection myAttrib1="Clowns">
<myChildSection
myChildAttrib1="Zippy"
myChildAttrib2="Michael Zawondy "/>
</myCustomSection>
<myCustomSection myAttrib1="Clowns2">
<myChildSection
myChildAttrib1="Zippy"
myChildAttrib2="Michael Zawondy "/>
</myCustomSection>
</myCustomGroup>

I got the same exception = "Sections must only appear once per config
file".
 
I succeeded in folowing MSDN example for
"ConfigurationCollectionAttribute" function with following app.config :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="VtrConnectionsSection"
type="VtrEngine.VtrConnectionsSection,
VtrEngine, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null" />
</configSections>
<VtrConnectionsSection>
<VtrConnections>
<Vtr ComPort="COM4" Name="DvcPro50 PTE" ID="1" />
<Vtr ComPort="COM2" Name="Vtr XT2 PTE" ID="2" />
</VtrConnections>
</VtrConnectionsSection>
</configuration>

Though I still have a little problem :
When the section <VtrConnections> contains no item, my custom
ConfigurationElement is still instantiated (as if a default is
instantiated).
In fact, when the ConfigurationElementCollection constructor is called,
it creates a ConfigurationElement.

Is the MSDN example bugged ?

Your help is very appreciated.
I didn't expected so much difficulties to read a few config lines !
 
Back
Top