The ConfigurationElement is an abstract class, so you cannot create an instance of it. It represents an XML element within a configuration file, such as the Web.config file.
Note: |
|---|
An element within a configuration file refers to a basic XML element or a section. The basic element is a simple XML tag with related attributes, if any. In its simplest form, a section coincides with a basic element. Complex sections can contain one or more basic elements, a collection of elements, and other sections.
|
The ConfigurationElement is used as the base class for the classes representing XML configuration elements, such as ConfigurationSection.
You can extend the ConfigurationElement class to represent a configuration element within a ConfigurationSection section. You can also create a ConfigurationElementCollection collection of ConfigurationElement elements, as shown in the Example section.
Handling Configuration
To handle configuration information using the standard types, use one of the following approaches:
Accessing a section. To access configuration information for your application you must use one of the GetSection methods provided by ConfigurationManager and WebConfigurationManager. For special sections such as appSettings and connectionStrings, you use the AppSettings property of the ConfigurationManager class or the AppSettings property of the WebConfigurationManager class, and the ConnectionStrings property of the ConfigurationManager class or the ConnectionStrings property of the WebConfigurationManager class. These methods perform read-only operations, use a single cached instance of the configuration, and are multithread aware.
Accessing configuration files. Your application can read and write configuration settings at any level, for itself or for other applications or computers as a whole, locally or remotely. You use one of the open methods provided by the ConfigurationManager class and the WebConfigurationManager class. These methods will return a Configuration object, which in turn provides the required methods and properties to handle the underlying configuration files. These methods perform read or write operations and recreate the configuration data every time a file is opened.
Advanced configuration. More advanced configuration handling is provided by the types SectionInformation, PropertyInformation, PropertyInformationCollection, ElementInformation, ContextInformation, ConfigurationSectionGroup, and ConfigurationSectionGroupCollection.
Extending Configuration Standard Types
You can also extend the standard configuration types such as ConfigurationElement, ConfigurationElementCollection, ConfigurationProperty, and ConfigurationSection, using either a programmatic or a declarative (attributed) model. For an example of how to extend a standard configuration type programmatically, see to the ConfigurationSection class. For an example of how to extend a standard configuration type using the attributed mode, see the ConfigurationElement class.
Notes to Implementers:
Configuration is the class that allows programmatic access for editing configuration files. You use one of the open methods provided by WebConfigurationManager for Web applications or by ConfigurationManager for client applications. These methods will return a Configuration object, which in turn provides the required methods and properties to handle the underlying configuration files. You can access these files for reading or writing as explained in the following sections:
Reading. You use GetSection or GetSectionGroup to read configuration information. Note that the user or process that reads must have the following permissions:
If your application needs read-only access to its own configuration, it is recommended that you use one of the GetSection overloaded methods in the case of Web applications. Or the GetSection method in the case of client applications.
These methods provide access to the cached configuration values for the current application, which has better performance than the Configuration class.
Note: |
|---|
If you use a static
GetSection method that takes a path parameter, the path parameter must refer to the application in which the code is running; otherwise, the parameter is ignored and configuration information for the currently-running application is returned.
|
Writing. You use one of the Save methods to write configuration information. Note that the user or process that writes must have the following permissions:
Notes to Inheritors:
Every ConfigurationElement object creates an internal ConfigurationPropertyCollection collection of ConfigurationProperty objects that represents either the element attributes or a collection of child elements.
Non-customizable information and functionality is contained by an ElementInformation object provided by the ElementInformation property.
You can use either a programmatic or a declarative (attributed) coding model to create a custom configuration element:
The programmatic model requires that for each element attribute, you create a property to get or set its value and add it to the internal property bag of the underlying ConfigurationElement base class.
The simpler declarative model, also called the attributed model, allows you to define an element attribute by using a property and then decorate it with attributes. These attributes instruct the ASP.NET configuration system about the property types and their default values. With this information, obtained through reflection, the ASP.NET configuration system creates the element property objects for you and performs the required initialization.