Solved

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

Posted on 2008-10-13
6
6,612 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

733 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