473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

multiple same section in app.config

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.

Sep 14 '06 #1
8 12059
Perhaps moving the ID into an attribute on <Connection>

I.E.:
<Connection ID="2">
<add key="ComPort" value="COM4" />
<add key="Name" value="Connection 2" />
</Connection>
Droopy wrote:
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.
Sep 14 '06 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Droopy" <dr**************@hotmail.comwrote in message
news:Xn**********************************@195.129. 110.72...
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.

Sep 14 '06 #3
I already tried your solution.
I got an exception "Sections must only appear once per config file".

"Sean Chambers" <dk****@gmail.comwrote in news:1158248668.638399.124870
@e3g2000cwe.googlegroups.com:
Perhaps moving the ID into an attribute on <Connection>

I.E.:
> <Connection ID="2">
<add key="ComPort" value="COM4" />
<add key="Name" value="Connection 2" />
</Connection>

Droopy wrote:
>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.NameValueSectionHandle r" />
</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.
Sep 15 '06 #4
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.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in news:eJ**************@TK2MSFTNGP02.phx.gbl:
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.

Sep 15 '06 #5
My problem is that I tried to use the same section multiple times.
A custom section won't help I suppose ?

Droopy <dr**************@hotmail.comwrote in
news:Xn**********************************@195.129. 110.71:
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.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
wrote
in news:eJ**************@TK2MSFTNGP02.phx.gbl:
>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.


Sep 15 '06 #6
Droopy,

Well, it will, but you have to have one root element that is not
repeated.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Droopy" <dr**************@hotmail.comwrote in message
news:Xn**********************************@195.129. 110.71...
My problem is that I tried to use the same section multiple times.
A custom section won't help I suppose ?

Droopy <dr**************@hotmail.comwrote in
news:Xn**********************************@195.129. 110.71:
>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.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
wrote
>in news:eJ**************@TK2MSFTNGP02.phx.gbl:
>>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.



Sep 15 '06 #7
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
news:uT**************@TK2MSFTNGP05.phx.gbl:
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".
Sep 18 '06 #8
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 <VtrConnectionscontains 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 !


Droopy <dr**************@hotmail.comwrote in
news:Xn**********************************@195.129. 110.71:
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
wrote in news:uT**************@TK2MSFTNGP05.phx.gbl:
>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".
Sep 18 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Craig | last post by:
I've developed an single sign on web application that we'd like to use with all the applications we're developing. The application's connection string is stored in web.config. I've gotten some...
4
by: Shrinivas | last post by:
Hi, Let us say my web.config is keep growing and I want to break the data logically and still use web.config methods to retrieve the data. e.g. <?xml version="1.0" encoding="utf-8" ?>...
6
by: mark | last post by:
I have an asp.net ecommerce web application on a remote web server. I'm using an Access database on the back end. I've notice a few strange things. When I mimic an multiple user environment by...
7
by: yurps | last post by:
Hello I have a couple of developers working on a web app. They all need to be able to change their local copy of the web.config file to run against their local databases. The copy in VSS needs to...
9
by: Graham | last post by:
I have been having some fun learning and using the new Controls and methods in .Net 2.0 which will make my life in the future easier and faster. Specifically the new databinding practises and...
5
by: Mike Kelly | last post by:
Hi. I've got a standard setup with a web app (ASP.NET 2.0) that I develop on my machine, deploy to a QA server for testing, and then on to a PROD environment. To facilitate the deployment...
0
by: mlafarlett | last post by:
Geez...seems like i've compared every friggin character...works n this programA but not n programB. I have a config file with multiple sections and when trying to read each section, it works great...
3
by: Sami Marzouki | last post by:
Hi, What I'm trying to do is: - To write a Web.config with custom sections. (Done) - To write a xsd schema for this custom sections.(Done) - Tell the Web.config to take the two schemas. When...
3
by: | last post by:
I'm seeking (probably basic) guidance on the right way to split a large site that's supposed to represent one domain(mydomain.org) into many small VS.NET projects, and how to avoid issues with...
2
by: Fresno Bob | last post by:
I quite like to store variables in the web.config file. However it can make the web.config messy and make deployment fiddly. Is there any way of have multiple web.config files - one with the major...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.