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
Solved

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

Posted on 2008-10-13
6
6,590 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
  • 4
6 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 22706631
0
 
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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