Export (0) Print
Expand All

ConfigurationElement Class

Represents an element in a configuration file.


Namespace:  Microsoft.Web.Administration
Assembly:  Microsoft.Web.Administration (in Microsoft.Web.Administration.dll)

public class ConfigurationElement

The ConfigurationElement type exposes the following members.

Protected methodConfigurationElementInitializes a new instance of the ConfigurationElement class.

Public propertyAttributesGets a configuration attribute collection that contains the list of attributes for this element.
Public propertyChildElementsGets all the child elements of the current element.
Public propertyElementTagNameGets the XML tag name of the current element.
Public propertyIsLocallyStoredGets a value indicating whether the configuration element is stored in a particular configuration file.
Public propertyItemGets or sets an attribute with the specified name.
Public propertyMethodsGets a collection of methods for the configuration element.
Public propertyRawAttributesGets the raw attribute names and values for the current configuration element.
Public propertySchemaGets the schema for the current element.

Public methodDelete
Public methodEquals (Inherited from Object.)
Protected methodFinalize (Inherited from Object.)
Public methodGetAttributeReturns a ConfigurationAttribute object that represents the requested attribute.
Public methodGetAttributeValueReturns the value of the specified attribute.
Public methodGetChildElement(String)Returns a child element that is under the current configuration element and has the specified name.
Public methodGetChildElement(String, Type)Returns a child element that is under the current configuration element and has the specified name and type.
Public methodGetCollection()()()()Returns the default collection for the current configuration element.
Public methodGetCollection(String)Returns all configuration elements that belong to the current configuration element.
Public methodGetCollection(Type)Returns the configuration element that has the specified type and is under the current configuration element.
Public methodGetCollection(String, Type)Returns the configuration element that has the specified name and type and is under the current configuration element.
Public methodGetHashCode (Inherited from Object.)
Public methodGetMetadataReturns metadata values from the element schema.
Public methodGetType (Inherited from Object.)
Protected methodMemberwiseClone (Inherited from Object.)
Public methodSetAttributeValueSets the value of the specified attribute.
Public methodSetMetadataSets metadata values from the element schema.
Public methodToString (Inherited from Object.)

This is the base class for many configuration entities, including configuration sections, collection entries, and nested elements in a section.

The following example implements several of the methods and properties of the ConfigurationElement class. This example obtains a ConfigurationSection object for the moduleProvider section and gets a ConfigurationElementCollection from that section. This example iterates through each ConfigurationElement of the retrieved collection.

[ModuleServiceMethod(PassThrough = true)]
public ArrayList GetElementCollection()
    // Use an ArrayList to transfer objects to the client.
    ArrayList arrayOfConfigPropertyBags = new ArrayList();

    ServerManager serverManager = new ServerManager();
    Configuration administrationConfig = serverManager.GetAdministrationConfiguration();
    ConfigurationSection moduleProvidersSection = 
    ConfigurationElementCollection elementCollection = 
    // If there is a configuration element with the name of TestDemo(Modified), delete it.
    ConfigurationElement elementtoremove = null;
    foreach (ConfigurationElement moduleproviderelement in elementCollection)
        if(moduleproviderelement.Attributes["name"].Value.ToString() == "TestDemo(Modified)")
            elementtoremove = moduleproviderelement;
    if (elementtoremove != null)
    foreach (ConfigurationElement moduleproviderelement in elementCollection)
        PropertyBag elementBag = new PropertyBag();
        elementBag[ConfigurationDemoGlobals.SchemaName] = 
        elementBag[ConfigurationDemoGlobals.ElementTagName] = 
        ArrayList attributeArrayList = new ArrayList();
        // Loop through all attributes in the element attribute list.
        // Get the attribute name and value.
        foreach (ConfigurationAttribute attribute in moduleproviderelement.Attributes)
            PropertyBag attributeBag = new PropertyBag();
            attributeBag[ConfigurationDemoGlobals.AttributeName] = attribute.Name;
            attributeBag[ConfigurationDemoGlobals.AttributeValue] = attribute.Value;
            // Change the value of the provider name "TestDemo" to "TestDemo(Modified)".
            if (attribute.Value.ToString() == "TestDemo")
                // Use any of the following lines to set the attribute value.
                // attribute.Value = "TestDemo(Modified)";
                    "name", "TestDemo(Modified)");
                // moduleproviderelement["name"] = "TestDemo(Modified)";
                // Use any of the following lines to retrieve the attribute value.
                // attributeBag[ConfigurationDemoGlobals.AttributeName] = "name";
                attributeBag[ConfigurationDemoGlobals.AttributeName] =
                // attributeBag[ConfigurationDemoGlobals.AttributeName] =
                //    moduleproviderelement["name"];
                // Set the element's lockItem attribute.
                moduleproviderelement.SetMetadata("lockItem", true); // persisted in Administration.config          

        elementBag[ConfigurationDemoGlobals.AttributeArrayList] = attributeArrayList;
    // Create a new element. It must have a unique name.
    ConfigurationElement newelement;
    bool newelementexists = false;
    foreach (ConfigurationElement element in elementCollection)
        if (element.Attributes["name"].Value.ToString() == "TestDemo")
            newelementexists = true;
    if (!newelementexists)
        newelement = elementCollection.CreateElement("add");
        newelement.Attributes["name"].Value = "TestDemo";
        newelement.Attributes["Type"].Value = "TestDemo.TestDemoModuleProvider, TestDemo, Version=, Culture=neutral, PublicKeyToken=104f78e73dc54601";
    // CommitChanges to persist the changes to Administration.config.
    return arrayOfConfigPropertyBags;

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

© 2015 Microsoft