<clear></clear> is not the same as <clear/>

In the dotNET configuration files you can add items to a collection like appSettings with

   <add key="" value=""/> 

Because this is XML you can also use the following line to add an item:

   <add key="" value=""></add>

But not the same for clearing the list. Try adding <clear></clear> to the top of your appSettings section and you likely get the exception “Unrecognized element ‘add'”.
According to Tess Ferrandez the workaround is so simple that Microsoft is not going to fix it.
Using reflector on the dotNET 2.0 system.configuration assembly I saw some strange code. Look at the OnDeserializeUnrecognizedElement of System.Configuration.ConfigurationElementCollection and in there you’ll see this part of code:

while (reader.MoveToNextAttribute())
   string name = reader.Name;
   throw new ConfigurationErrorsException(SR.GetString("Config_base_unrecognized_attribute", new object[] { name }), reader);

Reading this made me realize that the following xml will work, because there is no “unrecognized” tag after the closing clear tag.


So be safe and always use <clear/> in config files. Because this behavior affects all configuration. Remember this the next time you add a AuthorizationPolicy.

About erictummers

Working in a DevOps team is the best thing that happened to me. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Development and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.