I want to add custom configuration to app.config file as follows
<Companies> <Company name="" code=""/> <Company name="" code=""/> </Companies> <Employees> <Employee name="" Des="" addr="" sal=""/> <Employee name="" Des="" addr="" sal=""/> </Employeess> <Departments> <Department Id="" Projects=""/> </Departments> <Projects> <Project Path=""/> </Projects>
In the Department section, this refers to the Projects section.
Can someone tell me a way to do this? And how to access it in my code?
@Bhaskar: find the code for comments.
public class RegisterCompaniesConfig : ConfigurationSection { public static RegisterCompaniesConfig GetConfig() { return (RegisterCompaniesConfig)System.Configuration.ConfigurationManager.GetSection("RegisterCompanies")?? new RegisterCompaniesConfig(); } [System.Configuration.ConfigurationProperty("Companies")] public Companies Companies { get { object o = this["Companies"]; return o as Companies; } } } public class Companies : ConfigurationElementCollection { public Company this[int index] { get { return base.BaseGet(index) as Company; } set { if (base.BaseGet(index) != null) { base.BaseRemoveAt(index); } this.BaseAdd(index, value); } } protected override System.Configuration.ConfigurationElement CreateNewElement() { return new Company(); } protected override object GetElementKey(System.Configuration.ConfigurationElement element) { return ((Company)element).Name; } } public class Company : ConfigurationElement { [ConfigurationProperty("name", IsRequired = true)] public string Name { get { return this["name"] as string; } } [ConfigurationProperty("code", IsRequired = true)] public string Code { get { return this["code"] as string; } } }
Geeta
source share