Почему я не могу преобразовать атрибут во вложенный элемент?

Я читаю настройки из 'App.config'. Я только что разобрался, как работать с ConfigurationSection, ConfigurationElementCollection и ConfigurationelElement.

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration> 
    <configSections>
        <sectionGroup name="notificationSettingsGroup">
                <section name="mailTemplates" type="Project.Lib.Configuration.MailTemplateSection, Project.Lib"
                    allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" requirePermission="false"/>
        </sectionGroup>         
    </configSections>
    <notificationSettingsGroup>
        <mailTemplates>
            <items>
                <mailTemplate name="actionChain" subject="Subject bla-bla">
                    <body>Body bla-bla</body>
                </mailTemplate>                 
            </items>
        </mailTemplates>
    </notificationSettingsGroup>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>  
</configuration>

Мой C# код:

public class MailTemplateSection : ConfigurationSection
{
    [ConfigurationProperty("items", IsDefaultCollection = false)]
    public MailTemplateCollection MailTemplates
    {
        get { return (MailTemplateCollection)this["items"]; }
        set { this["items"] = value; }
    }
}

[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;
    }
}

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; }
    }
}

И рабочий код:

class Program
{
    static void Main(string[] args)
    {
        Configuration config =
            ConfigurationManager.OpenExeConfiguration(
            ConfigurationUserLevel.None);

        var mailTemplatesSection =
           config.GetSection("notificationSettingsGroup/mailTemplates") as MailTemplateSection;

    }
}

Все работает, когда я объявляю поля как атрибуты в xml. Но когда я пытаюсь преобразовать атрибуты во вложенный элемент - "Property 'Body' is not a ConfigurationElement" возникает ошибка.

Что я делаю не так?

6
задан Steven Jeuris 25 November 2011 в 12:51
поделиться