title: Custom configuration section with keys and an attribute
body: <p>I need to make a config section like appsettings but with an attribute on the surrounding element, eg:</p>
  
  <pre><code>&lt;userSettings&gt;
      &lt;machine name="my-machine"&gt;
          &lt;setting key="Site" value="http://localhost" /&gt;
          &lt;setting key="Online" value="true" /&gt;
      &lt;/machine&gt;
      &lt;!-- more machines --&gt;
  &lt;/userSettings&gt;
  </code></pre>
  
  <p>I have the collection of machines and collections of settings working ok as per examples, but I can't get the name attribute on machine to work in conjunction with settings collection.</p>
  
  <p>Here is my Machine class</p>
  
  <pre><code>public class Machine : ConfigurationElementCollection
  {
      [ConfigurationProperty("name", IsRequired = true)]
      public string Name
      {
          get
          {
              // This line doesn't compile, cannot convert SettingElement to string
              return (string)this["name"]; 
          }
      }
  
      protected override ConfigurationElement CreateNewElement()
      {
          return new SettingElement();
      }
  
      protected override object GetElementKey(ConfigurationElement element)
      {
          return ((SettingElement)element).Key;
      }
  
      // this method is the problem but I want to be able to do 
      // something like 
      // section.Machines[Environment.MachineName][settingKey].Value
      public new SettingElement this[string index]
      {
          get { return (SettingElement)BaseGet(index); }
          set
          {
              if (BaseGet(index) != null)
              {
                  BaseRemoveAt(BaseIndexOf(BaseGet(index)));
              }
              BaseAdd(value);
          }
      }
  }
  </code></pre>
  
  <p>And Setting</p>
  
  <pre><code>public class SettingElement : ConfigurationElement
  {
      [ConfigurationProperty("key", IsRequired = true, IsKey = true)]
      public string Key
      {
          get { return this["key"].ToString(); }
      }
  
      [ConfigurationProperty("value", IsRequired = true)]
      public string Value
      {
          get { return this["value"].ToString(); }
      }
  }
  </code></pre>