Hi,
You might want to use the built in configuration support for this.
Example :
namespace RuleConfig
{
public class RuleElement : ConfigurationElement
{
private static readonly ConfigurationProperty _order;
private static readonly ConfigurationProperty _name;
private static readonly ConfigurationPropertyCollection _properties;
static RuleElement()
{
_order = new ConfigurationProperty("order", typeof(int), null,
ConfigurationPropertyOptions.IsRequired);
_name = new ConfigurationProperty("name", typeof(string), null,
ConfigurationPropertyOptions.IsRequired);
_properties = new ConfigurationPropertyCollection();
_properties.Add(_order);
_properties.Add(_name);
}
public string Name
{
get { return (string)base[_name]; }
set { base[_name] = value; }
}
public int Order
{
get { return (int)base[_order]; }
set { base[_order] = value; }
}
protected override ConfigurationPropertyCollection Properties
{
get
{
return _properties;
}
}
}
[ConfigurationCollection(typeof(RuleElement), CollectionType =
ConfigurationElementCollectionType.BasicMap, AddItemName = "rule")]
public class RuleCollection : ConfigurationElementCollection
{
public void Add(RuleElement element)
{
base.BaseAdd(element);
}
protected override ConfigurationElement CreateNewElement()
{
return new RuleElement();
}
protected override object GetElementKey(ConfigurationElement
element)
{
return ((RuleElement)element).Order;
}
protected override string ElementName
{ get { return "rule"; } }
public override ConfigurationElementCollectionType CollectionType
{ get { return ConfigurationElementCollectionType.BasicMap; } }
}
public class RuleSection : ConfigurationSection
{
private static readonly ConfigurationProperty _rules;
private static readonly ConfigurationPropertyCollection _properties;
static RuleSection()
{
_rules = new ConfigurationProperty(null, typeof(RuleCollection),
null, ConfigurationPropertyOptions.IsDefaultCollection);
_properties = new ConfigurationPropertyCollection();
_properties.Add(_rules);
}
protected override ConfigurationPropertyCollection Properties
{
get
{
return _properties;
}
}
public RuleCollection Rules
{
get { return (RuleCollection)base[_rules]; }
}
}
}
-----------------------------------Example app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section type="RuleConfig.RuleSection, RuleConfig" name="rules"/>
</configSections>
<rules>
<rule order="1" name="name1"/>
<rule order="2" name="name2"/>
</rules>
</configuration>
----------------------------------Example of use
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
RuleSection section = config.GetSection("rules") as RuleSection;
if (section != null)
{
//read
foreach (RuleElement rule in section.Rules)
System.Console.WriteLine(string.Format("{0,5}:{1}", rule.Order,
rule.Name));
//modify
section.Rules.Add(new RuleElement() { Order = 8, Name = "name8" });
config.Save(ConfigurationSaveMode.Modified);
}
Hope You find this useful.
-Zsolt