简体   繁体   中英

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="LogAPIMessages" 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. Eg 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, eg 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> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM