How to loop through <configSections> elements?

E

Eve

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

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

The number of <section> elements 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
<configSections> elements. I would appreciate your help.
 
E

Eve

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.
 
E

Eve

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 <section> elements contained within
<configSections> to figure out the section name and pass it to
GetSectionSettings().
Sorry, I don't know how to be more specific than that.

Peter Duniho said:
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.

Sorry, I still don't fully understand the question. The code above seems
to be the code you want to use in order to retrieve the individual
sections named "1", "2", "3", etc. But that doesn't have anything to do
with how you're accessing the section containing those sections
("configSections"), or what the actual type of the ConfigurationSection is
(which would in turn affect what the easiest way to get at the individual
elements contained within would be).

Maybe someone else has enough experience with the ConfigurationManager
class to understand your question without more details. Otherwise, I
think that you'll have to post a concise-but-complete code sample that
illustrates exactly what you're doing.

Pete
 
E

Eve

Again, what I need is the code that figures our the name of each <section>
element within <configSections> ("1", "2", "3", etc.) by looping through all
<section> elements and passes the name to GetSectionSettings(), which in
turns retrieves values for the section that was passed to it.

Eve said:
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 <section> elements contained within
<configSections> to figure out the section name and pass it to
GetSectionSettings().
Sorry, I don't know how to be more specific than that.

Peter Duniho said:
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.

Sorry, I still don't fully understand the question. The code above seems
to be the code you want to use in order to retrieve the individual
sections named "1", "2", "3", etc. But that doesn't have anything to do
with how you're accessing the section containing those sections
("configSections"), or what the actual type of the ConfigurationSection is
(which would in turn affect what the easiest way to get at the individual
elements contained within would be).

Maybe someone else has enough experience with the ConfigurationManager
class to understand your question without more details. Otherwise, I
think that you'll have to post a concise-but-complete code sample that
illustrates exactly what you're doing.

Pete
 
E

Eve

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 <section> elements within
<configSections> because I HAVE NO CLUE HOW TO DO IT - that's why I posted my
question on this forum.
 
E

Eve

This is the extract from my app.config file:
<configSections>
<section name="1"
type="System.Configuration.DictionarySectionHandler" />
<section name="2"
type="System.Configuration.DictionarySectionHandler" />
<section name="3"
type="System.Configuration.DictionarySectionHandler" />
</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 <configSections> elements. 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 <configSections> elements.


Peter Duniho said:
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 <section> elements within
<configSections> because 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
 
E

Eve

I found this code on
http://www.codeproject.com/KB/cs/SystemConfiguration.aspx?display=Print that
loops through all section groups and sections in app.config. The only problem
that I'm having is group.IsDeclared is always false. Also, the group names
and section names are not returned in the order in which they are declared in
the config file, which is not a big deal as long as all of them are being
returned, I was just wondering why that's the case.

// Get the application configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// Get the collection of the section groups.
ConfigurationSectionGroupCollection sectionGroups = config.SectionGroups;

// Show the configuration values
ShowSectionGroupCollectionInfo(sectionGroups);

static void ShowSectionGroupCollectionInfo(
ConfigurationSectionGroupCollection sectionGroups)
{
ClientSettingsSection clientSection;
SettingValueElement value;

foreach(ConfigurationSectionGroup group in sectionGroups)
// Loop over all groups
{
if(!group.IsDeclared)
// Only the ones which are actually defined in app.config
continue;

Console.WriteLine("Group {0}", group.Name);

// get all sections inside group
foreach(ConfigurationSection section in group.Sections)
{
clientSection = section as ClientSettingsSection;
Console.WriteLine("\tSection: {0}", section);

if(clientSection == null)
continue;


foreach(SettingElement set in clientSection.Settings)
{
value = set.Value as SettingValueElement;
// print out value of each section
Console.WriteLine("\t\t{0}: {1}",
set.Name,value.ValueXml.InnerText);
}
}
}



Eve said:
This is the extract from my app.config file:
<configSections>
<section name="1"
type="System.Configuration.DictionarySectionHandler" />
<section name="2"
type="System.Configuration.DictionarySectionHandler" />
<section name="3"
type="System.Configuration.DictionarySectionHandler" />
</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 <configSections> elements. 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 <configSections> elements.


Peter Duniho said:
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 <section> elements within
<configSections> because 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
 
E

Eve

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.
Thanks!

Eve said:
I found this code on
http://www.codeproject.com/KB/cs/SystemConfiguration.aspx?display=Print that
loops through all section groups and sections in app.config. The only problem
that I'm having is group.IsDeclared is always false. Also, the group names
and section names are not returned in the order in which they are declared in
the config file, which is not a big deal as long as all of them are being
returned, I was just wondering why that's the case.

// Get the application configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// Get the collection of the section groups.
ConfigurationSectionGroupCollection sectionGroups = config.SectionGroups;

// Show the configuration values
ShowSectionGroupCollectionInfo(sectionGroups);

static void ShowSectionGroupCollectionInfo(
ConfigurationSectionGroupCollection sectionGroups)
{
ClientSettingsSection clientSection;
SettingValueElement value;

foreach(ConfigurationSectionGroup group in sectionGroups)
// Loop over all groups
{
if(!group.IsDeclared)
// Only the ones which are actually defined in app.config
continue;

Console.WriteLine("Group {0}", group.Name);

// get all sections inside group
foreach(ConfigurationSection section in group.Sections)
{
clientSection = section as ClientSettingsSection;
Console.WriteLine("\tSection: {0}", section);

if(clientSection == null)
continue;


foreach(SettingElement set in clientSection.Settings)
{
value = set.Value as SettingValueElement;
// print out value of each section
Console.WriteLine("\t\t{0}: {1}",
set.Name,value.ValueXml.InnerText);
}
}
}



Eve said:
This is the extract from my app.config file:
<configSections>
<section name="1"
type="System.Configuration.DictionarySectionHandler" />
<section name="2"
type="System.Configuration.DictionarySectionHandler" />
<section name="3"
type="System.Configuration.DictionarySectionHandler" />
</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 <configSections> elements. 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 <configSections> elements.


Peter Duniho said:
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 <section> elements within
<configSections> because 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
 

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