繁体   English   中英

从web.config文件中读取相同类型的嵌套配置节元素

[英]Reading nested configuration section element of same type from web.config file

好的,这是一个棘手的问题,或者我只是不知道该怎么做。

我必须解决创建和读取自定义配置部分的问题,例如:

<a>
 <b>
  <c/>
  <c/>
 </b>
</a>

我遇到的问题是使用ConfigurationManager.GetSection(“ a”)阅读以下配置:

<a>
 <b>
  <c>
   <c/>
  <c>
 </b>
</a>

有没有办法让这个工作?

谢谢。

我的建议? 放弃配置管理器并将配置加载到XDocument中。 假设您有一个如下所示的配置文件:

<Settings>
    <ApplicationSettings>
        <AppSetting1 Value="Test1" />
        <AppSetting2 Value="Test2" />
    </ApplicationSettings>
    <DeviceSettings>
        <DeviceSetting1 Value="Test3" />
        <DeviceSetting2 Value="Test4" />
    </DeviceSettings>
</Settings>

要从中获取值,可以将配置加载到XDocument中:

XDocument xdoc = XDocument.Load(@"Path\to\file.xml");

然后:

String test1 = xdoc.Element("Settings").Element("ApplicationSettings").Element("Appsetting1").Attribute("Name").Value;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM