我想创建一个自定义配置部分来处理像这样的网络文件
<reports>
<report name="abc">
<query name="que">
<parameter name="def" value="asd" type="string"> </parameter>
<parameter name="par" value="123" type="int"> </parameter>
</query>
</report>
<report name="abc">
<query name="que">
<parameter name="def" value="asd" type="string"> </parameter>
<parameter name="par" value="123" type="int"> </parameter>
</query>
</report>
</reports>
“报告”可能包含许多“报告”标记,“查询”可能包含许多“参数”标记。其他一切都应该只出现一次。
所以我创建了以下类结构:
以下是代码:
报告部分:
public class ReportSection : ConfigurationSection
{
// Create a "report" element.
[ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
[ConfigurationCollection(typeof(ReportElementCollection), AddItemName = "report")]
public ReportElementCollection Instances
{
get
{
return (ReportElementCollection)this[""];
}
}
public List<Report> GetAllValues()
{
List<Report> reports = new List<Report>();
foreach (ReportElement tags in (ReportElementCollection)this[""])
{
Report r = new Report(tags.Name, tags.Query.Name);//, tags.Query.Parameters);
//reports.Add(
reports.Add(r);
}
return reports;
}
}
ReportElementCollection:
public class ReportElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ReportElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
//set to whatever Element Property you want to use for a key
return ((ReportElement)element).Name;
}
}
ReportElement:
public class ReportElement : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public String Name
{
get
{
return (String)this["name"];
}
set
{
this["name"] = value;
}
}
// Create a "query" element.
[ConfigurationProperty("query", IsRequired=true)]
public QueryElement Query
{
get
{
return (QueryElement)this["query"];
}
set
{ this["query"] = value; }
}
}
QueryElement:
public class QueryElement : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public String Name
{
get
{
return (String)this["name"];
}
set
{
this["name"] = value;
}
}
// Create a "parameter" element.
[ConfigurationProperty("", IsDefaultCollection = true, IsRequired = true)]
public ParameterElementCollection Parameter
{
get
{
return (ParameterElementCollection)this[""];
}
}
}
ParameterElementCollection:
public class ParameterElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ParameterElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
//set to whatever Element Property you want to use for a key
return ((ParameterElement)element).Name;
}
}
ParameterElement
public class ParameterElement : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public String Name
{
get
{
return (String)this["name"];
}
set
{
this["name"] = value;
}
}
[ConfigurationProperty("value", IsRequired = true)]
public String Value
{
get
{
return (String)this["value"];
}
set
{
this["value"] = value;
}
}
[ConfigurationProperty("type", IsRequired = true)]
public String Type
{
get
{
return (String)this["type"];
}
set
{
this["type"] = value;
}
}
}
问题是,当程序尝试加载我的自定义部分时,它会一直运行到QueryElement.Parameter,此[“”]的count = 0。退出方法后,我收到以下错误消息:
无法识别的元素'参数'。
我正在阅读许多不同的资源,但它们似乎都符合我的代码。我想我错过了一些非常小的东西。你知道吗?
答案 0 :(得分:0)
我的webconfig不正确,正确的是
<query name="que">
<parameter>
<add name="def" value="asd" type="string">
<add name="par" value="123" type="int">
</parameter>
</query>
而不是
<query name="que">
<parameter name="def" value="asd" type="string"> </parameter>
<parameter name="par" value="123" type="int"> </parameter>
</query>