I want to create a custom config section that will handle a webcfg like this
<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>
Where "reports" may have many "report" tags and "query" may have many "parameter" tags. Everything else is should be present only once.
So I created the following class structure:
Here is the code:
Report Section:
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;
}
}
}
The problem is that when the program tries to load my custom section, it runs until QueryElement.Parameter, this[""] has count=0. After it exits the method, I get the following error message:
Unrecognized element 'parameter'.
I am reading many different resources, but they all seem to match my code. I think I am missing something very tinny. Do you know what?
My webconfig was incorrect, the correct would be
<query name="que">
<parameter>
<add name="def" value="asd" type="string">
<add name="par" value="123" type="int">
</parameter>
</query>
instead of
<query name="que">
<parameter name="def" value="asd" type="string"> </parameter>
<parameter name="par" value="123" type="int"> </parameter>
</query>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.