Вы можете сделать это с помощью файла конфигурации, подобного следующему (небольшое изменение по сравнению с приведенным выше):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="monitorSettings" type="RecursiveCustomConfiguration.MonitorConfig, RecursiveCustomConfiguration"/>
</configSections>
<monitorSettings>
<monitors>
<monitor description="description1" />
<monitor description="description2" />
<monitor description="description3">
<monitors>
<monitor description="description3.1" />
<monitor description="description3.2" />
</monitors>
</monitor>
</monitors>
</monitorSettings>
</configuration>
и классы конфигурации, такие как:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
namespace RecursiveCustomConfiguration
{
public class MonitorConfig : ConfigurationSection
{
public static MonitorConfig GetConfig()
{
return ConfigurationManager.GetSection("monitorSettings") as MonitorConfig;
}
[ConfigurationProperty("monitors", IsDefaultCollection = true, IsRequired = true)]
[ConfigurationCollection(typeof(MonitorCollection), AddItemName = "monitor")]
public MonitorCollection Monitors
{
get
{
return this["monitors"] as MonitorCollection;
}
}
}
public class MonitorCollection : ConfigurationElementCollection
{
public Monitor this[int index]
{
get
{
return base.BaseGet(index) as Monitor;
}
set
{
if (base.BaseGet(index) != null)
{
base.BaseRemoveAt(index);
}
this.BaseAdd(index, value);
}
}
public Monitor this[object description]
{
get
{
return base.BaseGet(description) as Monitor;
}
}
protected override ConfigurationElement CreateNewElement()
{
return new Monitor();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Monitor)element).Description;
}
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
}
protected override string ElementName
{
get { return "monitor"; }
}
}
public class Monitor : ConfigurationElement
{
[ConfigurationProperty("description", IsRequired = true)]
public string Description
{
get
{
return this["description"] as string;
}
}
[ConfigurationProperty("monitors", IsRequired = false)]
public MonitorCollection Monitors
{
get
{
return this["monitors"] as MonitorCollection;
}
}
}
}
Затем использовать в консольном приложении Windows, например:
MonitorCollection monitors = MonitorConfig.GetConfig().Monitors;
foreach (Monitor m in monitors)
{
Console.WriteLine(m.Description);
if (m.Monitors != null && m.Monitors.Count > 0)
{
foreach (Monitor m1 in m.Monitors)
{
Console.WriteLine(m1.Description);
}
}
}