AppSettings 清除 app.config 中的 Xml 元素
应用程序配置文件中
标记内的
XML 元素的用途是什么?
我看到它删除了以前添加的设置(请参阅下面的代码),但是为什么要这样做?
<appSettings>
<add key="LogInformation" value="False"/>
<add key="LogAPIMessages" value="False"/>
<add key="LogErrors" value="True"/>
<clear/> <!--This line removes previously added keys.-->
</appSettings>
我也明白,在代码中操作应用程序设置时,您可以清除现有的键,但是为什么有
XML 元素吗?
另外,出于习惯,我一直在前面放置
元素任何
元素。我需要这样做吗?我不应该这样做吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这适用于当一个 Web 应用程序位于另一个 Web 应用程序中时的情况。例如,您将博客作为主应用程序中的单独应用程序来实现。然后,在这种情况下,“clear”用于删除对继承的自定义应用程序设置的所有引用,这些设置是从父应用程序设置继承的。
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.
正如您已经想到的,需要删除以前定义的条目。此类条目不必与您自己的定义位于同一文件中。这也可能是从其他配置继承的条目,例如 machine.config。
尽管这对于
来说有点不寻常,但对于其他类似集合的配置元素(如
)来说,它的工作原理是相同的。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>
.