具有嵌套ConfigurationElementCollections的ConfigurationSection

时间:2013-10-19 02:37:36

标签: configurationsection

希望我可以将此问题提交给本网站的大脑信任,有人会看到我的错误。

我正在开发一个项目,其中电子邮件文本需要与各种内部类的属性中的信息“邮件合并”。电子邮件文本中的典型符号可能类似于“{member name},{mobile phone}等”

我想在web.config中使用ConfigurationSection定义符号和类。这是我建议的配置部分:

<EmailSymbols>
    <SymbolClasses>

        <SymbolClass name="OHMember">
            <Symbol name="Member Name" template="{0} {1}">
                <add index="0" value="SMFirstName" />
                <add index="1" value="SMLastName" />
            </Symbol>
            <Symbol name="Phone" template="{0}">
                <add index="0" value="SMPhone" />
            </Symbol>
        </SymbolClass>

        <SymbolClass name="Form">
            <Symbol name="Contact Name" dataname="ContactName" />
        </SymbolClass>

    </SymbolClasses>
</EmailSymbols>

...以及我试图解析它的代码:

public class EmailSymbols : ConfigurationSection {

    [ConfigurationProperty("SymbolClasses", IsRequired = true)]
    public SymbolClassCollection SymbolClasses {
        get {
            return this["SymbolClasses"] as SymbolClassCollection;
        }
    }
}

[ConfigurationCollection(typeof(SymbolClass), AddItemName = "SymbolClass")]
public class SymbolClassCollection : ConfigurationElementCollection {

    protected override ConfigurationElement CreateNewElement() {
        return new SymbolClass();
    }

    protected override object GetElementKey(ConfigurationElement element) {
        return ((SymbolClass)element).Name;
    }
}

[ConfigurationCollection(typeof(Symbol), AddItemName = "Symbol")]
public class SymbolClass : ConfigurationElementCollection {

    [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
    public String Name {
        get {
            return this["name"] as String;
        }
    }

    protected override ConfigurationElement CreateNewElement() {
        return new Symbol();
    }

    protected override object GetElementKey(ConfigurationElement element) {
        return ((Symbol)element).Name;
    }
}

[ConfigurationCollection(typeof(TemplateValue), AddItemName = "add")]
public class Symbol : ConfigurationElementCollection {

    [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
    public String Name {
        get {
            return this["name"] as String;
        }
    }

    [ConfigurationProperty("template", IsRequired = false)]
    public String Template {
        get {
            return this["template"] as String;
        }
    }

    [ConfigurationProperty("dataname", IsRequired = false)]
    public String DataName {
        get {
            return this["dataname"] as String;
        }
    }

    protected override ConfigurationElement CreateNewElement() {
        return new TemplateValue();
    }

    protected override object GetElementKey(ConfigurationElement element) {
        return ((TemplateValue)element).Index;
    }
}

public class TemplateValue : ConfigurationElement {

    [ConfigurationProperty("index", IsRequired = false, IsKey = true)]
    public Int32 Index {
        get {
            return this["index"] == null ? -1 : Convert.ToInt32(this["index"]);
        }
    }

    [ConfigurationProperty("value", IsRequired = false)]
    public String Value {
        get {
            return this["value"] as String;
        }
    }
}

当我使用此语句解析该部分时:     symbols = ConfigurationManager.GetSection(“EmailSymbols”)为EmailSymbols;

我收到此错误消息:“无法识别的元素'符号'。”

这只是.NET的一个方面,我不知道如何。任何人都可以给予的任何帮助都将非常感激。

我的XML定义是否有意义且是否正确?我想要一个SymbolClass集合,每个集合都包含一个Symbol集合,每个集合都包含一个TemplateValue集合。

再次感谢您的帮助。

最诚挚的问候, 麦

1 个答案:

答案 0 :(得分:1)

您可以尝试覆盖Init()类的SymbolClass方法:

protected override void Init()
{
    base.Init();
    this.AddElementName = "Symbol";
}

你也可以从类声明的上方删除[ConfigurationCollection(typeof(SymbolClass), AddItemName = "SymbolClass")]和其他类似的人,因为他们没有做任何事情。