0

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

我必须在创建和阅读自定义配置部分时遇到问题,例如:

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

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

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

有没有办法让这个工作?

谢谢你。

4

1 回答 1

0

我的建议?抛弃配置管理器并将配置加载到 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;
于 2013-08-07T20:33:25.750 回答