Here's a simple example.
Note, I had to put a space after the < of the xml and a space before the > of the xml to get it to show up.
//START CODE
//MyCompany.MyProject.csproj which results in MyCompany.MyProject.dll
//Add a Folder called "Configuration"
namespace MyCompany.MyProject.Configuration
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
public class TransformationToDirectoryMapping : ConfigurationElement
{
private const string FRIENDLY_NAME = "FriendlyName";
private const string PICKUP_FOLDER = "PickupFolder";
[ConfigurationProperty(FRIENDLY_NAME, DefaultValue = "", IsKey = false, IsRequired = true)]
public string FriendlyName
{
get
{
return ((string)(base[FRIENDLY_NAME]));
}
set
{
base[FRIENDLY_NAME] = value;
}
}
[ConfigurationProperty(PICKUP_FOLDER, DefaultValue = "", IsKey = true, IsRequired = true)]
public string PickupFolder
{
get
{
return ((string)(base[PICKUP_FOLDER]));
}
set
{
base[PICKUP_FOLDER] = value;
}
}
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
[ConfigurationCollection(typeof(TransformationToDirectoryMapping))]
public class TransformationToDirectoryMappingCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new TransformationToDirectoryMapping();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((TransformationToDirectoryMapping)(element)).PickupFolder;
}
public TransformationToDirectoryMapping this[int idx]
{
get
{
return (TransformationToDirectoryMapping)BaseGet(idx);
}
}
new public TransformationToDirectoryMapping this[string key]
{
get
{
return (TransformationToDirectoryMapping)BaseGet(key);
}
}
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
public class TransformationToDirectoryMappingConfigSection : ConfigurationSection
{
private const string TRANSFORMATION_TO_DIRECTORY_MAPPINGS = "TransformationToDirectoryMappings";
[ConfigurationProperty(TRANSFORMATION_TO_DIRECTORY_MAPPINGS)]
public TransformationToDirectoryMappingCollection TransformationToDirectoryMappingItems
{
get { return ((TransformationToDirectoryMappingCollection)(base[TRANSFORMATION_TO_DIRECTORY_MAPPINGS])); }
}
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
public static class MyRetriever
{
public static readonly string MAPPINGS_CONFIGURATION_SECTION_NAME = "TransformationToDirectoryMappingsSection";
public static TransformationToDirectoryMappingCollection GetTheCollection()
{
TransformationToDirectoryMappingConfigSection mappingsSection = (TransformationToDirectoryMappingConfigSection)ConfigurationManager.GetSection(MAPPINGS_CONFIGURATION_SECTION_NAME);
if (mappingsSection != null)
{
return mappingsSection.TransformationToDirectoryMappingItems;
}
return null; // OOPS!
}
}
}
//XML for config file:
< ?xml version="1.0" encoding="utf-8"? >
< configuration >
< configSections >
< section name="TransformationToDirectoryMappingsSection" type="MyCompany.MyProject.Configuration.TransformationToDirectoryMappingConfigSection, MyCompany.MyProject"/ >
< /configSections >
< TransformationToDirectoryMappingsSection >
< TransformationToDirectoryMappings >
< add FriendlyName="Hello" PickupFolder="C:\WUWUTemp\pickups\pickup11\" / >
< add FriendlyName="GoodBye" PickupFolder="C:\WUWUTemp\pickups\pickup12\" / >
< /TransformationToDirectoryMappings >
< /TransformationToDirectoryMappingsSection >
< /configuration >