Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用ConfigurationManager从App.config读取嵌套值_C#_App Config_Configurationmanager - Fatal编程技术网

C# 使用ConfigurationManager从App.config读取嵌套值

C# 使用ConfigurationManager从App.config读取嵌套值,c#,app-config,configurationmanager,C#,App Config,Configurationmanager,我有一个关于从App.configvia检索值的问题 这是我的App.config。我计划将这些值外包给一个printers.config,并通过printerOverrides configSource=“printers.config”/>将这些值引入 <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="

我有一个关于从
App.config
via检索值的问题

这是我的
App.config
。我计划将这些值外包给一个
printers.config
,并通过
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>
但是,我也无法通过从SectionGroup元素中的节检索数据


我的XML无效吗?或者我需要什么来获取SectionGroup中嵌套节的值尽管配置中的XML有效,但配置本身无效

节组配置不支持重复元素(每个元素必须唯一并单独指定)。另外,
host
元素不能有任何属性

通过使用如下配置,您可以(某种程度上)实现您想要的:



通过今天的大量尝试,我得出了相同的结论。我现在尝试实现我的节处理程序。基本上,我会回到一个简单的部分,其中的键看起来像。。。所以我可以在处理程序中进行检查。定义多个部分是不可行的,因为我将有10多个主机。
var test = ConfigurationManager.GetSection("test") as NameValueCollection;
Debug.WriteLine(test["key1"]);
var test = ConfigurationManager.GetSection("machine1") as NameValueCollection;
Debug.WriteLine(test["defaultPrinter"]);
var test = ConfigurationManager.GetSection("printerOverrides/machine1") as NameValueCollection;
Debug.WriteLine(test["defaultprinter"]);
var test = ConfigurationManager.GetSection("printerOverrides/host1") as NameValueCollection;
Debug.WriteLine(test["defaultprinter"]);