17

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>

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

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

Here are the App.config and example.config files

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

<?xml version="1.0"?>
<example>
    <add key="version" value="blahblah" />
</example>
Aaroninus
  • 1,062
  • 2
  • 17
  • 35
chaitanya
  • 1,591
  • 2
  • 24
  • 39
  • If you post code, XML or data samples, **please** highlight those lines in the text editor and click on the "code samples" button ( `{ }` ) on the editor toolbar to nicely format and syntax highlight it! If you post error messages, **please** use the blockquotes ( ` " ` ) to properly format the error message. – marc_s May 12 '11 at 15:49
  • Thanks, I will keep that in mind the next time – chaitanya May 12 '11 at 15:55
  • It seems you're put your `configSource=` on the `` tag, but in reality, you've externalized the contents of your `` tag....... – marc_s May 12 '11 at 15:59

4 Answers4

11

I got the same error. In my case is due that I have keys in two files, then detect appSettings tag as duplicated.

if you need conserve some project related keys in web.config and your personalized key in another file (recommended for security reasons), use file property instead of configSource.

web.config file:

<configuration>
  <appSettings file="../AppSettings.config">
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
</configuration>

AppSettings.config file:

<?xml version="1.0"?>

<appSettings>
  <add key="RutaBodega" value="D:\Test\Card"/>
  <add key="CodeLen" value="5"/>
</appSettings>

Hope it help others!

Nasenbaer
  • 4,810
  • 11
  • 53
  • 86
equiman
  • 7,810
  • 2
  • 45
  • 47
4

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).

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." />

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." />

Can you try with this example.config ??

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • marc I actually ran the code and it was giving me this exception. Here is the image of the error http://imageshack.us/photo/my-images/840/errorgz.jpg/. I actually went through lots of Internet search and I found out I wwas doing the right thing, but it just wouldn't run – chaitanya May 12 '11 at 15:54
  • @chaitanya: when you put your node into an external config file, you **cannot** totally change your structure!! You need to have exactly the same structure as you had before in your first app.config - see my update.... – marc_s May 12 '11 at 15:57
  • Sweet. Thanks marc it works. What was wrong with the key and value structure. Does the add tag correspond to whenever you have a tag nested inside the example tag – chaitanya May 12 '11 at 16:04
  • You use an `` when that config section you're dealing with has such a structure - e.g. ``. This has nothing in particular to do with putting your config into an external file.. – marc_s May 12 '11 at 16:14
3

My problem is that I had a was adding a configSource AND a key in the same tag.

Incorrect:

<appSettings configSource="Appsettings.config">
    <add key="Setting1" value="May 5, 2014"/>
</appSettings>

If you delete the "add" tag or move it into your configSource file, the error goes away.

Correct:

<appSettings configSource="Appsettings.config" />
Brett Pennings
  • 1,489
  • 14
  • 19
1

If the section you are making external is defined in configSections, you should place the configSource attribute in the element defining the section. Only the appSettings and connectionStrings sections (which don't need definitions in configSections) should have tags with configSource in the body of the main config file.

KeithS
  • 70,210
  • 21
  • 112
  • 164
  • Keith I just have tags for appSettings, if you would look in the Changed App.config file and I think I have placed the configSource attribute in the element "example" – chaitanya May 12 '11 at 15:57
  • 2
    Not true - **any** configuration section can be externalized using a `configSource=` attribute - even if VS complains about it .... – marc_s May 12 '11 at 15:58
  • Partially helped. Since only the externalized custom section throws error. I had to define with ```
    ```
    – guneysus Nov 01 '15 at 10:36