ConfigurationElementCollection Class

Note: This class is new in the .NET Framework version 2.0.

Represents a configuration element containing a collection of child elements.

Namespace: System.Configuration
Assembly: System.Configuration (in system.configuration.dll)

Public MustInherit Class ConfigurationElementCollection
	Inherits ConfigurationElement
	Implements ICollection, IEnumerable
Dim instance As ConfigurationElementCollection

public abstract class ConfigurationElementCollection extends ConfigurationElement implements ICollection, IEnumerable
public abstract class ConfigurationElementCollection extends ConfigurationElement implements ICollection, IEnumerable

The ConfigurationElementCollection represents a collection of elements within a configuration file.


An element within a configuration file refers to a basic XML element or a section. A simple element is an XML tag with related attributes, if any. A simple element constitutes a section. Complex sections can contain one or more simple elements, a collection of elements, and other sections.

You use the ConfigurationElementCollection to work with a collection of ConfigurationElement objects. Implement this class to add collections of custom ConfigurationElement elements to a ConfigurationSection.

Notes to Implementers You can use 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 and set its value, and that you add it to the internal property bag of the underlying ConfigurationElement base class. The declarative model, also referred to as the attributed model, allows you to define an element attribute by using a property and configuring it with attributes. These attributes instruct the ASP.NET configuration system about the property types and their default values. ASP.NET can use reflection to obtain this information and then create the element property objects and perform the required initialization.

The following code example shows how to implement a custom ConfigurationElementCollection. For examples of related custom types, see ConfigurationSection and ConfigurationElement.

Imports System
Imports System.Configuration
Imports System.Collections

' Define the UrlsCollection that contains 
' UrlsConfigElement elements.

Public Class UrlsCollection
    Inherits ConfigurationElementCollection
    Public Sub New() 
        Dim url As UrlConfigElement = _
        CType(CreateNewElement(), UrlConfigElement)
        ' Add the element to the collection.
    End Sub 'New

    Public Overrides ReadOnly Property CollectionType() _
    As ConfigurationElementCollectionType
            Return ConfigurationElementCollectionType.AddRemoveClearMap
        End Get
    End Property

    Protected Overloads Overrides Function CreateNewElement() _
    As ConfigurationElement
        Return New UrlConfigElement()

    End Function 'CreateNewElement
    Protected Overloads Overrides Function CreateNewElement( _
    ByVal elementName As String) _
    As ConfigurationElement
        Return New UrlConfigElement(elementName)

    End Function 'CreateNewElement
    Protected Overrides Function GetElementKey( _
    ByVal element As ConfigurationElement) As [Object]
        Return CType(element, UrlConfigElement).Name

    End Function 'GetElementKey
    Public Shadows Property AddElementName() As String 
            Return MyBase.AddElementName
        End Get 
            MyBase.AddElementName = value
        End Set 
    End Property

    Public Shadows Property ClearElementName() As String 
            Return MyBase.ClearElementName
        End Get 
            MyBase.AddElementName = value
        End Set 
    End Property

    Public Shadows ReadOnly Property RemoveElementName() As String 
            Return MyBase.RemoveElementName
        End Get 
    End Property 

    Public Shadows ReadOnly Property Count() As Integer 
            Return MyBase.Count
        End Get 
    End Property
    Default Public Shadows Property Item( _
    ByVal index As Integer) As UrlConfigElement
            Return CType(BaseGet(index), UrlConfigElement)
        End Get
        Set(ByVal value As UrlConfigElement)
            If Not (BaseGet(index) Is Nothing) Then
            End If
            BaseAdd(index, value)
        End Set
    End Property

    Default Public Shadows ReadOnly Property Item( _
    ByVal Name As String) As UrlConfigElement
            Return CType(BaseGet(Name), UrlConfigElement)
        End Get
    End Property

    Public Function IndexOf( _
    ByVal url As UrlConfigElement) As Integer
        Return BaseIndexOf(url)

    End Function 'IndexOf

    Public Sub Add(ByVal url As UrlConfigElement) 
        ' Add custom code here.
    End Sub 'Add

    Protected Overrides Sub BaseAdd( _
    ByVal element As ConfigurationElement)
        BaseAdd(element, False)
        ' Add custom code here.
    End Sub 'BaseAdd

    Public Overloads Sub Remove( _
    ByVal url As UrlConfigElement)
        If BaseIndexOf(url) >= 0 Then
        End If

    End Sub 'Remove

    Public Sub RemoveAt(ByVal index As Integer) 
    End Sub 'RemoveAt

    Overloads Public Sub Remove(ByVal name As String) 
    End Sub 'Remove    

    Public Sub Clear() 
    End Sub 'Clear ' Add custom code here.
End Class 'UrlsCollection

The following configuration excerpt is used by the previous example.

<?xml version="1.0" encoding="utf-8"?>
    <section name="MyUrls"
      type="Samples.AspNet.UrlsSection, ConfigurationElement, Version=, Culture=neutral, PublicKeyToken=null" 
      restartOnExternalChanges="true" />
  <MyUrls lockAllElementsExcept="urls">
      name="Microsoft" url="" port="0" />
        <clear />
          name="Microsoft" url="" port="0"
          lockAllAttributesExcept="port" />
          name="Contoso" url="" port="8080"
          lockAllAttributesExcept="port" lockItem="true" />

       Derived Classes

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

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions