Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to loop through <sectionGroup> elements in app.config file?

Posted on 2008-10-13
6
Medium Priority
?
6,807 Views
Last Modified: 2012-05-05
The <configSections> in my app.config file consists of multiple sectionGroups. The program that reads my app.config needs to retrieve values from each section within each sectionGroup. Each section will consists of the same key names (with different values). The program does not know how many sectionGroups there will be and how many <section> elements each sectionGroup will consist of. That's why it needs to loop through all sectionGroups within <configSections> and all sections within <sectionGroup>. I searched on the internet and in different forums for an answer, but still can't figure out how to do that. I would appreciate your help. Thank you.

  <configSections>
    <sectionGroup name="Group1">
      <section name="1" type="System.Configuration.DictionarySectionHandler" />
      <section name="2" type="System.Configuration.DictionarySectionHandler" />
      <section name="3" type="System.Configuration.DictionarySectionHandler" />
    </sectionGroup>
    <sectionGroup name="Group2">
      <section name="1" type="System.Configuration.DictionarySectionHandler" />
      <section name="2" type="System.Configuration.DictionarySectionHandler" />
      <section name="3" type="System.Configuration.DictionarySectionHandler" />
    </sectionGroup>
 </configSections>
0
Comment
Question by:EwS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 16

Expert Comment

by:Gyanendra Singh
ID: 22708784
0
 

Author Comment

by:EwS
ID: 22711247
I appreciate your quick response, but this is not what I'm looking for. For instance, the following line of code:
NameValueCollection servers = (NameValueCollection) ConfigurationManager.GetSection("mySectionGroup/mySection");
will retrieve the section for me and then I can loop through its key-value pairs, but I don't what to hard-code the sectionGroup name and the section name - I want to be able to retrieve them dynamically by looping through all the elements contained within <configSections> (just like I'm looping through the key-value pairs for each section). The second link (by BondinASP) also has the names hard-coded.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:EwS
ID: 22721022
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);
              }
        }
    }
0
 

Author Comment

by:EwS
ID: 22824297
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!
0
 

Accepted Solution

by:
EwS earned 0 total points
ID: 22871284
I found a solution to my problem. I changed the way I retrieve the names of section groups and sections from my app.config file. I'm using xml, which is less elegant, but more straightforward (I don't need to loop through section groups that are not declared, and section groups and sections are returned in the order they are defined.

Since I found a solution that works myself, can I, please, get my 500 points back?
Thank you.
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question