微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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 ??

原文地址:https://www.jb51.cc/csharp/100092.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐