c# – 在configSource中使用外部.config文件会产生错误

我正在玩如何使用Configuration Manager在App.config文件中读取/写入C#中的 WPF应用程序的自定义部分.我在 .NET 2.0 Configuration Demystified阅读了这篇优秀文章,它帮助我使用配置文件.这是我写的初始App.config文件,它工作正常.

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="example" type="CustomConfig.ExampleSection,CustomConfig" />
  </configSections>
  <example version="A sample string value." />
  <appSettings>
    <add key="version_string" value="1.01" />
  </appSettings>
</configuration>

但是,当我更改App.config文件时,我的自定义部分将从configSource中提到的外部配置文件中读取,Visual Studio给出了一个错误

The format of a configSource file must be an element containing the name
of the section.

这是App.config和example.config文件

更改了App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="example" type="CustomConfig.ExampleSection,CustomConfig" />
  </configSections>
  <example configSource="example.config" />
  <appSettings>
    <add key="version_string" value="1.01" />
  </appSettings>
</configuration>

example.config

<?xml version="1.0"?>
<example>
    <add key="version" value="blahblah" />
</example>

解决方法

Visual Studio的编辑器/ intellisense有一个缺点,它抱怨configSource =属性 – 但它绝对合法,它确实有效;我每天都在各种生产系统中使用它.

我的建议:试试吧! :-)运行代码 – 我很确定它会起作用(你的配置看起来不错).

更新:好的 – 似乎你完全改变了< example>的风格标签.在您原来的app.config中,您有:

<example version="A sample string value." />

当然,您的外化example.config必须包含相同的值和相同的结构:

<?xml version="1.0"?>
<example version="A sample string value." />

你可以尝试这个example.config ??

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...