AppSettings Clear Xml element in app.config
What is the purpose of the <Clear \>
XML element within the <AppSettings>
tag in an application's config file?
I see it removes previously added settings (see code below), but why would you want to do that?
<appSettings>
<add key="LogInformation" value="False"/>
<add key="LogAPIM开发者_StackOverflowessages" value="False"/>
<add key="LogErrors" value="True"/>
<clear/> <!--This line removes previously added keys.-->
</appSettings>
I also understand that when manipulating app settings in code you could clear existing keys, but why have the <clear\>
XML element?
Also, out of habit really, i've been putting the <clear\>
element before any <add>
elements. Do i need to do this, should i not be doing this?
This is applicable in scenarios when you have a web application within another web application. E.g you implement blog as a seperate application within your main application. Then in such cases "clear" is used to remove all references to inherited custom application settings, which are inherited from the parent application settings.
As you already figured, it is ment to remove previously defined entries. Such entries don't have to be in the same file as your own definitions. That could also be entries inherited from other configurations, e.g. machine.config.
Albeit that would be a little unusual for <appSettings>
in particular, it works the same for other collection-like configuration elements like <connectionStrings>
.
精彩评论