希望我能把这个问题提交给这个网站的智囊团,这样别人就会看到我的错误。
我在一个项目中工作,其中电子邮件文本需要与在各种内部类的属性中找到的信息进行“邮件合并”。电子邮件文本中的典型符号可能类似于“{成员姓名}、{移动电话}等”。
我想使用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>...and我试图用来解析它的代码:
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") as EmailSymbols;
我收到这个错误消息:“无法识别的元素‘Symbol’。”
这只是一个我不熟悉的.NET领域。任何人所能提供的任何帮助都将不胜感激。
我的XML定义有意义吗?它的格式是否正确?我想要一个SymbolClass集合,每个集合包含一个Symbol集合,每个集合包含一个TemplateValue集合。
再次感谢您的帮助。
致以最好的问候,吉米
发布于 2014-03-05 23:28:20
您可以尝试覆盖SymbolClass类的Init()方法:
protected override void Init()
{
base.Init();
this.AddElementName = "Symbol";
}你也可以从上面的类声明中删除[ConfigurationCollection(typeof(SymbolClass), AddItemName = "SymbolClass")]和其他类似的东西,因为它们什么都不做。
https://stackoverflow.com/questions/19461749
复制相似问题