title: Accessing a Custom Configuration Section in .Net
body: <p>I am trying to access settings in my config file, which is a series of xml elements listed as such:</p>
  
  <pre><code>&lt;databases&gt;
  &lt;database name="DatabaseOne" Value="[value]" /&gt;
  &lt;database name="DatabaseTwo" Value="[value]" /&gt;
  &lt;/databases&gt;
  </code></pre>
  
  <p>Now I want to access it. I have set up classes like so:</p>
  
  <pre><code>Public Class DatabaseConfigurationHandler
      Inherits ConfigurationSection
  
      &lt;ConfigurationProperty("Databases", IsDefaultCollection:=True)&gt; _
     Public ReadOnly Property Databases() As DatabaseCollection
          Get
              Return CType(Me("Databases"), DatabaseCollection)
          End Get
      End Property
  End Class
  
  Public Class DatabaseCollection
      Inherits ConfigurationElementCollection
  
      Protected Overloads Overrides Function CreateNewElement() As ConfigurationElement
          Return (New Database())
      End Function
  
      Protected Overloads Overrides Function GetElementKey(ByVal element As ConfigurationElement) As Object
          Return (CType(element, Database).DatabaseName)
      End Function
  
  End Class
  
  Public Class Database
      Inherits ConfigurationElement
  
      &lt;ConfigurationProperty("name", IsKey:=True, IsRequired:=True)&gt; _
         Public Property DatabaseName() As String
          Get
              Return Me("name").ToString()
          End Get
          Set(ByVal Value As String)
              Me("name") = Value
          End Set
      End Property
  
  
      &lt;ConfigurationProperty("value", IsRequired:=True)&gt; _
  Public Property DatabaseValue() As String
          Get
              Return Me("value").ToString()
          End Get
          Set(ByVal Value As String)
              Me("value") = Value
          End Set
      End Property
  
  End Class
  </code></pre>
  
  <p>I want to be able get the element by it's name and return the value but I can't see to do that:</p>
  
  <pre><code>Dim config As New DatabaseConfigurationHandler
                  config = System.Configuration.ConfigurationManager.GetSection("databases/database")
                  Return config.Databases("DatabaseOne")
  </code></pre>
  
  <p>Am I missing some code, what am I doing wrong? Any other errors in the above?</p>
  
  <p>Thanks.</p>