Sebastian
Sebastian

Reputation: 281

Reading nested values from App.config with ConfigurationManager

I have a question about retrieving values from the App.config via ConfigurationManager.

This is my App.config. I plan to outsource the values to a printers.config and pull the values in via printerOverrides configSource="printers.config" />.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
     <sectionGroup name="printerOverrides">
        <section name="host" type="System.Configuration.NameValueSectionHandler" />
      </sectionGroup>
      <section name="test" type="System.Configuration.NameValueSectionHandler"/>
  </configSections>  

  <startup> 
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <printerOverrides>
    <host name="machine1">
      <add key="defaultPrinter" value="Overridden" />
    </host>
    <host name="machine2">
      <add key="defaultPrinter" value="Overridden" />
      <add key="otherSetting" value="OtherSettingValue" />
    </host>
  </printerOverrides>
  <test>
    <add key="key1" value="value1" />
  </test>
</configuration>

I am able to get the values from the <test>-Section without any issues with this snippet:

var test = ConfigurationManager.GetSection("test") as NameValueCollection;
Debug.WriteLine(test["key1"]);

But I can not retrieve the data from the section in the SectionGroup element either via

var test = ConfigurationManager.GetSection("machine1") as NameValueCollection;
Debug.WriteLine(test["defaultPrinter"]);

or

var test = ConfigurationManager.GetSection("printerOverrides/machine1") as NameValueCollection;
Debug.WriteLine(test["defaultprinter"]);

Is my XML invalid? Or what do I need to get the values for the nested section inside the SectionGroup

Upvotes: 2

Views: 7994

Answers (1)

codersl
codersl

Reputation: 2332

Although the XML in your config is valid, the configuration itself is invalid.

The section group configuration does not support repeating elements (each element must be unique and specified separately). Also the host element cannot have any attributes.

You can (kind of) achieve what you want by using a configuration like this:

<configSections>
  <sectionGroup name="printerOverrides">
    <section name="host1" type="System.Configuration.NameValueSectionHandler" />
    <section name="host2" type="System.Configuration.NameValueSectionHandler" />
  </sectionGroup>
</configSections>  

<printerOverrides>
  <host1>
    <add key="defaultPrinter" value="Overridden" />
  </host1>
  <host2>
    <add key="defaultPrinter" value="Overridden" />
    <add key="otherSetting" value="OtherSettingValue" />
  </host2>  
</printerOverrides> 

Then this will work:

var test = ConfigurationManager.GetSection("printerOverrides/host1") as NameValueCollection;
Debug.WriteLine(test["defaultprinter"]);

If this doesn't suit your needs, then you will need to create custom configuration section classes. See How to create custom config section in app.config?

Upvotes: 5

Related Questions