在 configSource 中使用外部 .config 文件会产生错误 [英] Using an external .config file in configSource produces error
问题描述
我正在研究如何使用配置管理器为 C# 中的 WPF 应用程序读取/写入 App.config 文件中的自定义部分.我阅读了这篇关于 .NET 2.0 Configuration Demystified 的优秀文章,它帮助了我很多使用配置文件.这是我编写的初始 App.config 文件,它运行良好.
I was playing around with how to use the Configuration Manager to read/write custom sections in the App.config file for a WPF application in C#. I read this excellent article on .NET 2.0 Configuration Demystified and it helped me a lot in using the config files. Here is the initial App.config file which I wrote and it works fine.
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 给我一个错误
But when I changed the App.config file in such a way that my custom section will be read from an external config file mentioned in configSource, Visual Studio gives me an error
configSource 文件的格式必须是包含名称的元素部分.
The format of a configSource file must be an element containing the name of the section.
这里是 App.config 和 example.config 文件
Here are the App.config and example.config files
更改了 App.config
Changed 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
example.config
<?xml version="1.0"?>
<example>
<add key="version" value="blahblah" />
</example>
推荐答案
Visual Studio 的编辑器/智能感知有一个缺点,它会抱怨 configSource=
属性 - 但它绝对合法,而且它确实有效;我每天都在各种生产系统中使用它.
Visual Studio's editor/intellisense has a shortcoming in that it complains about the configSource=
attribute - but it is absolutely legal, and it does work; I use it every day, in various production systems.
我的建议:试试吧!:-) 运行代码 - 我很确定它会工作(你的配置对我来说看起来不错).
My recommendation: just try it! :-) Run the code - I'm pretty sure it will work (your configs look OK to me).
更新: 好的 - 看来您完全改变了 <example>
标签的样式.在您原来的 app.config
中,您有:
Update: OK - well it seems you're totally changing the style of the <example>
tag. In your original app.config
you have:
<example version="A sample string value." />
当然,您的外部化example.config
必须包含相同的值和相同 结构:
So of course, your externalized example.config
must contain the same values and the same structure:
<?xml version="1.0"?>
<example version="A sample string value." />
你能试试这个 example.config
吗??
Can you try with this example.config
??
这篇关于在 configSource 中使用外部 .config 文件会产生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!