Using the appSettings Element
The easiest way to add configuration
settings to a configuration file is to use the
appSettings
element. Configuration settings added
this way take the form of key-value pairs. These key-value pairs may
be added anywhere in the hierarchy of configuration files. Once
added, key-value pairs can be removed individually in a configuration
file lower in the hierarchy, or the entire set can be cleared.
Example 15-1 shows an excerpt from a machine
configuration file, with an appSettings
element
added.
<?xml version="1.0" encoding="UTF-8" ?> <configuration> ... <appSettings> <add key="some key" value="some value" /> </appSettings> ... </configuration>
This configuration file simply adds a configuration setting whose key is “some key” and whose value is “some value”. The added key will be available to all .NET applications running on this machine.
Example 15-2 shows an application configuration file
that uses the appSettings
element to remove the
key added in the machine configuration file and add another key.
<?xml version="1.0" encoding="UTF-8" ?> <configuration> <appSettings> <remove key="some key" /> <add key="some other key" value="some other value" /> </appSettings> </configuration>
The application that loads this application configuration file will no longer have the key-value pair with key “some key”, but it will have a key-value pair with key “some ...
Get .NET & XML now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.