Share via


ConfigurationElement Class

Definition

Represents an element in a configuration file.

public ref class ConfigurationElement
public class ConfigurationElement
type ConfigurationElement = class
Public Class ConfigurationElement
Inheritance
ConfigurationElement
Derived

Examples

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 = 
        administrationConfig.GetSection("moduleProviders");
    ConfigurationElementCollection elementCollection = 
        moduleProvidersSection.GetCollection();
    // 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)
    {
        elementCollection.Remove(elementtoremove);
    }
    foreach (ConfigurationElement moduleproviderelement in elementCollection)
    {
        PropertyBag elementBag = new PropertyBag();
        elementBag[ConfigurationDemoGlobals.SchemaName] = 
            moduleproviderelement.Schema.Name;
        elementBag[ConfigurationDemoGlobals.ElementTagName] = 
            moduleproviderelement.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)";
                moduleproviderelement.SetAttributeValue(
                    "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] =
                    moduleproviderelement.GetAttributeValue("name");
                // attributeBag[ConfigurationDemoGlobals.AttributeName] =
                //    moduleproviderelement["name"];
                // Set the element's lockItem attribute.
                moduleproviderelement.SetMetadata("lockItem", true); // persisted in Administration.config          

            }
            attributeArrayList.Add(attributeBag);
        }
        elementBag[ConfigurationDemoGlobals.AttributeArrayList] = attributeArrayList;
        arrayOfConfigPropertyBags.Add(elementBag);
    }
    // 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=1.0.0.0, Culture=neutral, PublicKeyToken=104f78e73dc54601";
        elementCollection.Add(newelement);
    }
    // CommitChanges to persist the changes to Administration.config.
    serverManager.CommitChanges();
    return arrayOfConfigPropertyBags;
}

Remarks

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

Constructors

ConfigurationElement()

Initializes a new instance of the ConfigurationElement class.

Properties

Attributes

Gets a configuration attribute collection that contains the list of attributes for this element.

ChildElements

Gets all the child elements of the current element.

ElementTagName
IsLocallyStored

Gets a value indicating whether the configuration element is stored in a particular configuration file.

Item[String]

Gets or sets an attribute with the specified name.

Methods

Gets a collection of methods for the configuration element.

RawAttributes
Schema

Gets the schema for the current element.

Methods

Delete()
GetAttribute(String)

Returns a ConfigurationAttribute object that represents the requested attribute.

GetAttributeValue(String)

Returns the value of the specified attribute.

GetChildElement(String, Type)

Returns a child element that is under the current configuration element and has the specified name and type.

GetChildElement(String)

Returns a child element that is under the current configuration element and has the specified name.

GetCollection()

Returns the default collection for the current configuration element.

GetCollection(String, Type)

Returns the configuration element that has the specified name and type and is under the current configuration element.

GetCollection(String)

Returns all configuration elements that belong to the current configuration element.

GetCollection(Type)

Returns the configuration element that has the specified type and is under the current configuration element.

GetMetadata(String)

Returns metadata values from the element schema.

SetAttributeValue(String, Object)

Sets the value of the specified attribute.

SetMetadata(String, Object)

Sets metadata values from the element schema.

Applies to