Solved

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

Posted on 2008-10-13
6
6,548 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
what technologies offer Authentication over Web Services? 4 104
Achieve json result 2 65
COnsume rest client 6 36
coded character set iptc field within a jpeg 1 68
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now