title: Why can't I convert attribute to nested element?
body: <p>I'm reading settings from 'App.config'. I just figured out how to work with <code>ConfigurationSection</code>, <code>ConfigurationElementCollection</code> and <code>ConfigurationelElement</code>.</p>
  
  <p>App.config:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt; 
      &lt;configSections&gt;
          &lt;sectionGroup name="notificationSettingsGroup"&gt;
                  &lt;section name="mailTemplates" type="Project.Lib.Configuration.MailTemplateSection, Project.Lib"
                      allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" requirePermission="false"/&gt;
          &lt;/sectionGroup&gt;         
      &lt;/configSections&gt;
      &lt;notificationSettingsGroup&gt;
          &lt;mailTemplates&gt;
              &lt;items&gt;
                  &lt;mailTemplate name="actionChain" subject="Subject bla-bla"&gt;
                      &lt;body&gt;Body bla-bla&lt;/body&gt;
                  &lt;/mailTemplate&gt;                 
              &lt;/items&gt;
          &lt;/mailTemplates&gt;
      &lt;/notificationSettingsGroup&gt;
      &lt;startup&gt;
          &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/&gt;
      &lt;/startup&gt;  
  &lt;/configuration&gt;
  </code></pre>
  
  <p>My C# code:</p>
  
  <pre><code>public class MailTemplateSection : ConfigurationSection
  {
      [ConfigurationProperty("items", IsDefaultCollection = false)]
      public MailTemplateCollection MailTemplates
      {
          get { return (MailTemplateCollection)this["items"]; }
          set { this["items"] = value; }
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>[ConfigurationCollection(typeof(MailTemplateElement), AddItemName = "mailTemplate",
      CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
  public class MailTemplateCollection : ConfigurationElementCollection
  {
      protected override ConfigurationElement CreateNewElement()
      {
          return new MailTemplateElement();
      }
  
      protected override object GetElementKey(ConfigurationElement element)
      {
          return ((MailTemplateElement) element).Name;
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public class MailTemplateElement : ConfigurationElement
  {
      [ConfigurationProperty("name", DefaultValue = "action", IsKey = true, IsRequired = true)]
      public string Name
      {
          get { return (string)this["name"]; }
          set { this["name"] = value; }
      }
  
      [ConfigurationProperty("subject", DefaultValue = "Subject", IsKey = false, IsRequired = true)]
      public string Subject
      {
          get { return (string)this["subject"]; }
          set { this["subject"] = value; }
      }
  
      [ConfigurationProperty("body", DefaultValue = "Body", IsKey = false, IsRequired = true)]
      public string Body
      {
          get { return (string)this["body"]; }
          set { this["body"] = value; }
      }
  }
  </code></pre>
  
  <hr>
  
  <p>And working code:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          Configuration config =
              ConfigurationManager.OpenExeConfiguration(
              ConfigurationUserLevel.None);
  
          var mailTemplatesSection =
             config.GetSection("notificationSettingsGroup/mailTemplates") as MailTemplateSection;
  
      }
  }
  </code></pre>
  
  <p>All works, when I'm declaring fields as attributes in xml. But when I try to convert attributes into nested element - <em>"Property 'Body' is not a ConfigurationElement"</em> error occurs.</p>
  
  <p>What am I doing wrong?</p>