I need to do something like this in my user settings section:
ConfigurationManager.ConnectionStrings["mongodb"]
The "mongodb" line above is the key that I use to access an element of type System.Configuration.ConnectionStringSettings. I want to do the same with my collection:
[ConfigurationCollection(typeof(Question))]
public class QuestionCollection : ConfigurationElementCollection
{
public override bool IsReadOnly()
{
return false;
}
protected override ConfigurationElement CreateNewElement()
{
return new Question();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Question)element).id;
}
public Question this[int idx]
{
get {
return (Question)BaseGet(idx);
}
set
{
if (BaseGet(idx) != null)
BaseRemoveAt(idx);
BaseAdd(idx, value);
}
}
}
I was interested that the method described above is a way to get what I want ... But I do not know how .... The type of key that I want to use for access is an integer.
Suppose I have the following configuration:
<securityQuestions>
<questions>
<add id="3" value="What is your name?" default="true"/>
<add id="4" value="What is your age?"/>
</questions>
</securityQuestions>
How can I access the first element (id = 3) using ... Section.Questions [3] (3 is not a position, but a key)?