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

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

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

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

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

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.

<appSettings>
   <clear></clear>
</appSettings>

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

My work as a recruited developer changes almost every month. 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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s