DO NOT FORGET IMPORT System.Configuration.dll
APP.CONFIG
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name ="FutureActivityGroup">
<section name="FutureActivities" type="ConfigSectionForAgent.FutureActivityConfigHandler, ConfigSectionForAgent"/>
</sectionGroup>
</configSections>
<FutureActivityGroup>
<FutureActivities>
<FutureActivity id="001" service ="AFE" passengerImportance="HC"/>
<FutureActivity id="002" service ="CSA" passengerImportance="HC"/>
</FutureActivities>
</FutureActivityGroup>
</configuration>
PROGRAM
namespace ConfigSectionForAgent
{
class Program
{
static void Main(string[] args)
{
FutureActivityConfigHandler config = (FutureActivityConfigHandler) System.Configuration. ConfigurationManager.GetSection("FutureActivityGroup/FutureActivities");
}
}
}
CONFIG HANDLER
namespace ConfigSectionForAgent
{
public class FutureActivityConfigHandler : ConfigurationSection
{
public FutureActivityConfigHandler()
{
}
[ConfigurationProperty("", IsDefaultCollection = true, IsKey = false, IsRequired = true)]
public FutureActivityCollection FutureActivities
{
get
{
return base[""] as FutureActivityCollection;
}
}
}
}
CONFIGURATION COLLECTION
namespace ConfigSectionForAgent
{
public class FutureActivityCollection : ConfigurationElementCollection
{
public FutureActivityElement this[int index]
{
get { return BaseGet(index) as FutureActivityElement; }
}
protected override string ElementName
{
get
{
return "FutureActivity";
}
}
protected override bool IsElementName(string elementName)
{
return !String.IsNullOrEmpty(elementName) && elementName == ElementName;
}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
#region overrided methods
///<summary>
///When overridden in a derived class, creates a new <see cref="T:System.Configuration.ConfigurationElement"></see>.
///</summary>
///
///<returns>
///A new <see cref="T:System.Configuration.ConfigurationElement"></see>.
///</returns>
///
protected override ConfigurationElement CreateNewElement()
{
return new FutureActivityElement();
}
///<summary>
///Gets the element key for a specified configuration element when overridden in a derived class.
///</summary>
///
///<returns>
///An <see cref="T:System.Object"></see> that acts as the key for the specified <see cref="T:System.Configuration.ConfigurationElement"></see>.
///</returns>
///
///<param name="element">The <see cref="T:System.Configuration.ConfigurationElement"></see> to return the key for. </param>
protected override object GetElementKey(ConfigurationElement element)
{
return ((FutureActivityElement)element).Id;
}
#endregion
}
}
CONFIGURATION ELEMENT
namespace ConfigSectionForAgent
{
public class FutureActivityElement : ConfigurationElement
{
[ConfigurationProperty("id", IsRequired=true, IsKey=true)]
public string Id
{
get { return ((string)base["id"]); }
set { base["id"] = value; }
}
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\", MaxLength = 3)]
[ConfigurationProperty("service", IsRequired = true)]
public string Service
{
get { return ((string)base["service"]); }
set { base["service"] = value; }
}
//[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 2)]
[ConfigurationProperty("passengerImportance", IsRequired = true)]
public string PassengerImportance
{
get { return ((string)base["passengerImportance"]); }
set { base["passengerImportance"] = value; }
}
}
}