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