Export (0) Print
Expand All

ConfigurationElement Class

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)

'Declaration
Public Class ConfigurationElement
'Usage
Dim instance As ConfigurationElement

The ConfigurationElement type exposes the following members.

  NameDescription
Protected methodConfigurationElementInitializes a new instance of the ConfigurationElement class.
Top

  NameDescription
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.
Top

  NameDescription
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.)
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.

' Methods
<ModuleServiceMethod(PassThrough:=True)> _
Public Function GetElementCollection() As ArrayList
    '' Use an ArrayList to transfer objects to the client.
    Dim arrayOfConfigPropertyBags As New ArrayList
    Dim serverManager As New ServerManager
    Dim elementCollection As ConfigurationElementCollection = serverManager.GetAdministrationConfiguration.GetSection("moduleProviders").GetCollection
    '' If there is a configuration element with the name of TestDemo(Modified), delete it.
    Dim elementtoremove As ConfigurationElement = Nothing
    Dim moduleproviderelement As ConfigurationElement
    For Each moduleproviderelement In elementCollection
        If (moduleproviderelement.Attributes.Item("name").Value.ToString = "TestDemo(Modified)") Then
            elementtoremove = moduleproviderelement
        End If
    Next

    If (Not elementtoremove Is Nothing) Then
        elementCollection.Remove(elementtoremove)
    End If
    Dim moduleproviderelement As ConfigurationElement
    For Each moduleproviderelement In elementCollection
        Dim elementBag As New PropertyBag
        elementBag.Item(2) = moduleproviderelement.Schema.Name
        elementBag.Item(3) = moduleproviderelement.ElementTagName
        Dim attributeArrayList As New ArrayList
        '' Loop through all attributes in the element attribute list.
        '' Get the attribute name and value.
        Dim attribute As ConfigurationAttribute
        For Each attribute In moduleproviderelement.Attributes
            Dim attributeBag As New PropertyBag
            attributeBag.Item(0) = attribute.Name
            attributeBag.Item(1) = attribute.Value
            If (attribute.Value.ToString = "TestDemo") Then
                '' Use any of the following lines to set the attribute value.
                '' attribute.Value = "TestDemo(Modified)"
                moduleproviderelement.SetAttributeValue("name", "TestDemo(Modified)")
                '' moduleproviderelement.Item("name") = "TestDemo(Modified)"
                '' Use any of the following lines to retrieve the attribute value.
                attributeBag.Item(0) = "name"
                '' attributeBag.Item(0) = moduleproviderelement.GetAttributeValue("name")
                '' attributeBag.Item(0) = moduleproviderelement.Item("name")
                '' Set the element's lockItem attribute.
                moduleproviderelement.SetMetadata("lockItem", True)
            End If

            attributeArrayList.Add(attributeBag)
        Next
        elementBag.Item(4) = attributeArrayList
        arrayOfConfigPropertyBags.Add(elementBag)
    Next
    '' Create a new element. It must have a unique name.
    Dim newelementexists As Boolean = False
    Dim element As ConfigurationElement
    For Each element In elementCollection
        If (element.Attributes.Item("name").Value.ToString = "TestDemo") Then
            newelementexists = True
        End If
    Next
    If Not newelementexists Then
        Dim newelement As ConfigurationElement = elementCollection.CreateElement("add")
        newelement.Attributes.Item("name").Value = "TestDemo"
        newelement.Attributes.Item("Type").Value = "TestDemo.TestDemoModuleProvider, TestDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=104f78e73dc54601"
        elementCollection.Add(newelement)
    End If
    '' CommitChanges to persist the changes to Administration.config.
    serverManager.CommitChanges()
    Return arrayOfConfigPropertyBags
End Function


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

ADD
Show:
© 2015 Microsoft