在configSource中使用外部.config文件会产生错误 [英] Using an external .config file in configSource produces error
问题描述
我一直在研究如何使用配置管理器为C#中的WPF应用程序读取/写入App.config文件中的自定义部分。我在 .NET 2.0配置神秘化上阅读了这篇出色的文章,它对我有帮助很多使用配置文件。这是我编写的初始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
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文件的格式必须是包含名称$的元素该部分的b $ b。
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屋!