473,404 Members | 2,114 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,404 software developers and data experts.

How to loop through <configSections> elements?

I have the following sections defined in my app.config file:

<configSections>
<section name="1"
type="System.Configuration.DictionarySectionHandle r" />
<section name="2"
type="System.Configuration.DictionarySectionHandle r" />
<section name="3"
type="System.Configuration.DictionarySectionHandle r" />
</configSections>

The number of <sectionelements is not set, but each section will contain
the same key names. I need to get a name of each section (1, 2, 3) to read
the key-value pairs for each section. I can't figure out how to loop through
<configSectionselements. I would appreciate your help.
Oct 7 '08 #1
7 6172
On Tue, 07 Oct 2008 13:36:06 -0700, Eve <Ev*@discussions.microsoft.com>
wrote:
I have the following sections defined in my app.config file:

<configSections>
<section name="1"
type="System.Configuration.DictionarySectionHandle r" />
<section name="2"
type="System.Configuration.DictionarySectionHandle r" />
<section name="3"
type="System.Configuration.DictionarySectionHandle r" />
</configSections>

The number of <sectionelements is not set, but each section will
contain
the same key names. I need to get a name of each section (1, 2, 3) to
read
the key-value pairs for each section. I can't figure out how to loop
through
<configSectionselements. I would appreciate your help.
What does the code that you are currently using to read the app.config
file look like?

It's hard to suggest an alternative or improvement on what you're doing
without actually knowing what you're doing.

Pete

Oct 7 '08 #2
I have a method that accepts a section name as a parameter and retrieves its
settings like this:

public void GetSectionSettings(string sectionName)
{
IDictionary configTable = (IDictionary)ConfigurationManager.GetSection
(sectionName);
string dayOrMonth = configTable["DayOrMonth"].ToString();
...
}

What I need is the code that loops through <configSections<section>
elements and passes the section name (such as "1") to GetSectionSettings
method. I could not find any code on the web that would do that.
"Peter Duniho" wrote:
On Tue, 07 Oct 2008 13:36:06 -0700, Eve <Ev*@discussions.microsoft.com>
wrote:
I have the following sections defined in my app.config file:

<configSections>
<section name="1"
type="System.Configuration.DictionarySectionHandle r" />
<section name="2"
type="System.Configuration.DictionarySectionHandle r" />
<section name="3"
type="System.Configuration.DictionarySectionHandle r" />
</configSections>

The number of <sectionelements is not set, but each section will
contain
the same key names. I need to get a name of each section (1, 2, 3) to
read
the key-value pairs for each section. I can't figure out how to loop
through
<configSectionselements. I would appreciate your help.

What does the code that you are currently using to read the app.config
file look like?

It's hard to suggest an alternative or improvement on what you're doing
without actually knowing what you're doing.

Pete

Oct 7 '08 #3
On Wed, 08 Oct 2008 06:33:02 -0700, Eve <Ev*@discussions.microsoft.com>
wrote:
The code inside GetSectionSettings method retrieves the individual
sections
named "1", "2", "3". The thing is I DO NOT KNOW the names of those
sections.
That's why I need to loop through all <sectionelements contained within
<configSectionsto figure out the section name and pass it to
GetSectionSettings().
Sorry, I don't know how to be more specific than that.
You can be more specific by posting the code. A concise-but-complete code
sample would unambiguously explain exactly what is going on.
Oct 8 '08 #4
Again, this is my code for GetSectionSettings method that accepts a section
name as a parameter and retrieves the section's settings:
public void GetSectionSettings(string sectionName)
{
IDictionary configTable = (IDictionary)ConfigurationManager.GetSection
(sectionName);
string dayOrMonth = configTable["DayOrMonth"].ToString();
...
}
I DON'T have any code that loops through <sectionelements within
<configSectionsbecause I HAVE NO CLUE HOW TO DO IT - that's why I posted my
question on this forum.

"Peter Duniho" wrote:
On Wed, 08 Oct 2008 06:33:02 -0700, Eve <Ev*@discussions.microsoft.com>
wrote:
The code inside GetSectionSettings method retrieves the individual
sections
named "1", "2", "3". The thing is I DO NOT KNOW the names of those
sections.
That's why I need to loop through all <sectionelements contained within
<configSectionsto figure out the section name and pass it to
GetSectionSettings().
Sorry, I don't know how to be more specific than that.

You can be more specific by posting the code. A concise-but-complete code
sample would unambiguously explain exactly what is going on.
Oct 8 '08 #5
On Wed, 08 Oct 2008 12:12:19 -0700, Eve <Ev*@discussions.microsoft.com>
wrote:
Again, this is my code for GetSectionSettings method that accepts a
section
name as a parameter and retrieves the section's settings:
>public void GetSectionSettings(string sectionName)
{
IDictionary configTable = (IDictionary)ConfigurationManager.GetSection
(sectionName);
string dayOrMonth = configTable["DayOrMonth"].ToString();
...
}
I don't see any way for that method to work generally. It can only work
for sections that implement the IDictionary interface, and you haven't
explained why the containing section does, never mind what type is
returned by that section.
I DON'T have any code that loops through <sectionelements within
<configSectionsbecause I HAVE NO CLUE HOW TO DO IT - that's why I
posted my
question on this forum.
I'm not asking for the code that you can't write. But you haven't even
posted a concise-but-complete code sample of what _does_ work.

Please see http://www.yoda.arachsys.com/csharp/complete.html and
http://www.yoda.arachsys.com/csharp/incomplete.html
Oct 8 '08 #6
This is the extract from my app.config file:
<configSections>
<section name="1"
type="System.Configuration.DictionarySectionHandle r" />
<section name="2"
type="System.Configuration.DictionarySectionHandle r" />
<section name="3"
type="System.Configuration.DictionarySectionHandle r" />
</configSections>

<1>
<add key="DayOrMonth" value="M" />
<add key="NoOfDaysOrMonths" value="3" />
</1>

<2>
<add key="DayOrMonth" value="M" />
<add key="NoOfDaysOrMonths" value="2" />
</2>

<3>
<add key="DayOrMonth" value="D" />
<add key="NoOfDaysOrMonths" value="14" />
</3>

GetSectionSettings() code that I posted DOES WORK for me. I simply perform
this call:
GetSectionSettings("1");
GetSectionSettings("2");
GetSectionSettings("3");

and the method gets values for each element in each section (M and 3 for
section "1", M and 2 for section "2", D and 14 for section "3") and does some
processing based on those values.

I don't want to hard-code "1", "2", "3" when I call GetSectionSettings
because there could be 50 sections, not just 3, and I don't need my program
to know what the names of those sections are, I want the program to figure
that out by looping through <configSectionselements. It shouldn't be even
necessary for me to post GetSectionSettings() code because it's got NOTHING
to do with how I would loop through <configSectionselements.
"Peter Duniho" wrote:
On Wed, 08 Oct 2008 12:12:19 -0700, Eve <Ev*@discussions.microsoft.com>
wrote:
Again, this is my code for GetSectionSettings method that accepts a
section
name as a parameter and retrieves the section's settings:
public void GetSectionSettings(string sectionName)
{
IDictionary configTable = (IDictionary)ConfigurationManager.GetSection
(sectionName);
string dayOrMonth = configTable["DayOrMonth"].ToString();
...
}

I don't see any way for that method to work generally. It can only work
for sections that implement the IDictionary interface, and you haven't
explained why the containing section does, never mind what type is
returned by that section.
I DON'T have any code that loops through <sectionelements within
<configSectionsbecause I HAVE NO CLUE HOW TO DO IT - that's why I
posted my
question on this forum.

I'm not asking for the code that you can't write. But you haven't even
posted a concise-but-complete code sample of what _does_ work.

Please see http://www.yoda.arachsys.com/csharp/complete.html and
http://www.yoda.arachsys.com/csharp/incomplete.html
Oct 8 '08 #7
On Tue, 28 Oct 2008 10:50:01 -0700, Eve <Ev*@discussions.microsoft.com>
wrote:
Does anyone know the answer to my question? I would expect to get some
help
from this site, but nobody provided me with the appropriate code - the
one
that I found myself is only partially working - nobody from all the
experts
knows the solution? I don't mean to sound rude, I'm just looking for
help.
Contrary to what one might think from many of the messages here, this
newsgroup is a) not populated by people who know every last thing there is
to know about .NET, and b) is not really even for .NET programming
questions.

It's specifically for questions about C#, and by implicit community
agreement, questions about .NET wind up answered as people are able too.
But when you're doing something that is outside the normal realm of
experience, it's not too surprising that no one has a direct, easy answer
to your question.

That becomes especially true when you cannot be bothered to post a
concise-but-complete code example that reliably demonstrates your
problem. People who are experienced but not experts can often help work
through a problem. But they are not likely to invest any time or effort
in doing so when the person posting the question won't do their own part.

Pete
Oct 28 '08 #8

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

Similar topics

0
by: Faisal | last post by:
I have a question regarding the "PublicKeyToken" attribute of the "<section>" tag in the web.config file. Pretty much most references have this value set to "b77a5c561934e089"; the same value as in...
0
by: anonieko | last post by:
Add a reference to log4net.dll 1: In the APP.CONFIG write the following: <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="log4net"...
2
by: Joseph Geretz | last post by:
Parser Error Message: Only one <configSections> element allowed. It must be the first child element of the root <configuration> element. OK, fine, easy enough to fix, I just need to copy and...
1
by: Joseph Geretz | last post by:
Why isn't the ASP.NET runtime providing a valid reference for this object to my WebService? I'm trying to use WSE and DIME to send attachments via Web Service. I've provided a whole lot of...
2
by: Vikram | last post by:
I am using log4net in a vb.net console application. But its not working. I have added congif setting in app.config file also No error is generated but app. does not write to log file. ...
5
by: Rainer Queck | last post by:
Hello NG, Is it possible to share the settings of an application with a class libreary? In my case I have a application and a set of different reports (home made) put into a class library. The...
9
by: Milsnips | last post by:
Hi all. i'm tryng to implement the Rewrite.NET url rewritining functionality into a test project i've created, however i am hitting a problem at this line (direct from the web example): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.