0 out of 2 rated this helpful - Rate this topic

ConfigurationElement Class

IIS 7.0

Represents an element in a configuration file.

System..::..Object
  Microsoft.Web.Administration..::..ConfigurationElement
    More...

Namespace:  Microsoft.Web.Administration
Assembly:  Microsoft.Web.Administration (in Microsoft.Web.Administration.dll)
public class ConfigurationElement

The ConfigurationElement type exposes the following members.

  Name Description
Protected method ConfigurationElement Initializes a new instance of the ConfigurationElement class.
Top
  Name Description
Public property Attributes Gets a configuration attribute collection that contains the list of attributes for this element.
Public property ChildElements Gets all the child elements of the current element.
Public property ElementTagName Gets the XML tag name of the current element.
Public property IsLocallyStored Gets a value indicating whether the configuration element is stored in a particular configuration file.
Public property Item Gets or sets an attribute with the specified name.
Public property Methods Gets a collection of methods for the configuration element.
Public property RawAttributes Gets the raw attribute names and values for the current configuration element.
Public property Schema Gets the schema for the current element.
Top
  Name Description
Public method Delete
Public method Equals (Inherited from Object.)
Protected method Finalize (Inherited from Object.)
Public method GetAttribute Returns a ConfigurationAttribute object that represents the requested attribute.
Public method GetAttributeValue Returns the value of the specified attribute.
Public method GetChildElement(String) Returns a child element that is under the current configuration element and has the specified name.
Public method GetChildElement(String, Type) Returns a child element that is under the current configuration element and has the specified name and type.
Public method GetCollection()()()() Returns the default collection for the current configuration element.
Public method GetCollection(String) Returns all configuration elements that belong to the current configuration element.
Public method GetCollection(Type) Returns the configuration element that has the specified type and is under the current configuration element.
Public method GetCollection(String, Type) Returns the configuration element that has the specified name and type and is under the current configuration element.
Public method GetHashCode (Inherited from Object.)
Public method GetMetadata Returns metadata values from the element schema.
Public method GetType (Inherited from Object.)
Protected method MemberwiseClone (Inherited from Object.)
Public method SetAttributeValue Sets the value of the specified attribute.
Public method SetMetadata Sets metadata values from the element schema.
Public method ToString (Inherited from Object.)
Top

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 = 
        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;
}


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ