After digging around, I haven't found anything related to this question.
Below is my current code:
Configuration File
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="customSettings" type="CustomAssembly.Configuration.CustomSettingsGroup, CustomAssmbly, Version=1.0.0.0, Culture=neutral">
<section name="services" type="CustomAssmbly.Configuration.ServiceSection, Agrione.Common45, Version=1.0.0.0, Culture=neutral" requirePermission="false" allowLocation="true" allowDefinition="Everywhere" />
</sectionGroup>
</configSections>
<customSettings>
<services>
<service>
<add name="AgrioneApi"
isDefault="true"
serviceType="1"
url="http://localhost:8094/" />
</service>
</services>
</customSettings>
</configuration>
CustomSettingsGroup
public class CustomSettingsGroup : ConfigurationSectionGroup
{
[ConfigurationProperty("services", IsDefaultCollection = true)]
public ServiceSection ServicesSection => Sections["services"] as ServiceSection;
}
ServiceSection Class
public class ServiceSection : ConfigurationSection
{
[ConfigurationProperty("service", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(BaseConfigCollection<ServiceConfigElement>), AddItemName = "add")]
public BaseConfigCollection<ServiceConfigElement> Services => (BaseConfigCollection<ServiceConfigElement>)base["service"];
public ServiceConfigElement GetDefault()
{
var result = (from ServiceConfigElement e in Services
where e.IsDefault
select e).FirstOrDefault() ?? Services[0];
return result;
}
}
BaseConfigCollection Class
public class BaseConfigCollection<TConfigElement> : ConfigurationElementCollection
where TConfigElement : BaseConfigElement, new()
{
public new TConfigElement this[string name] => (TConfigElement)BaseGet(name);
public TConfigElement this[int index]
{
get => (TConfigElement)BaseGet(index);
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
public void Add(TConfigElement element)
{
BaseAdd(element);
}
public void Clear()
{
BaseClear();
}
protected override ConfigurationElement CreateNewElement()
{
return new TConfigElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((TConfigElement)element).Name;
}
public void Remove(TConfigElement element)
{
BaseRemove(element);
}
public void RemoveAt(int index)
{
BaseRemoveAt(index);
}
public void Remove(string name)
{
BaseRemove(name);
}
}
What I want to accomplish, is not having, on the configuration file, that "add" in the "service" element. So, I would like to have below configuration file, without the "add".
Any help? Thank you in advance.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="customSettings" type="CustomAssembly.Configuration.CustomSettingsGroup, CustomAssmbly, Version=1.0.0.0, Culture=neutral">
<section name="services" type="CustomAssmbly.Configuration.ServiceSection, Agrione.Common45, Version=1.0.0.0, Culture=neutral" requirePermission="false" allowLocation="true" allowDefinition="Everywhere" />
</sectionGroup>
</configSections>
<customSettings>
<services>
<service name="AgrioneApi"
isDefault="true"
serviceType="1"
url="http://localhost:8094/" />
</services>
</customSettings>
</configuration>