<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="deviceFieldConfig" type="ConfigurationSectionDemo.DeviceFieldonfig,ConfigurationSectionDemo" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<deviceFieldConfig deviceCode = "BC2D00">
<fields>
<add fieldName="BCSP2D" fieldId="10032"/>
<add fieldName="BCCZ2D" fieldId="10033"/>
<add fieldName="BCQJ2D" fieldId="10034"/>
<add fieldName ="BCWG2D" fieldId="10035"/>
<add fieldName ="JCJCSJ" fieldId="10065"/>
</fields>
</deviceFieldConfig>
</configuration>
public class DeviceFieldonfig : ConfigurationSection
{
[ConfigurationProperty("deviceCode", IsRequired = true)]
public string DeviceCode { get { return (string)base["deviceCode"]; } set { base["deviceCode"] = value; } }
[ConfigurationProperty("fields", IsDefaultCollection =false )]
public FiledCollection Fileds { get { return (FiledCollection)this["fields"]; } set { base["fields"] = value; } }
}
public class FiledCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new FieldElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
FieldElement fieldElement = element as FieldElement;
return fieldElement?.FieldName;
}
public FieldElement this[int index]
{
get { return (FieldElement)BaseGet(index); }
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
public new string this[string key]
{
get { return ((FieldElement)base.BaseGet(key)).FieldId; }
}
}
public class FieldElement : ConfigurationElement
{
[ConfigurationProperty("fieldName", IsRequired = true, IsKey = true)]
public string FieldName { get { return (string)base["fieldName"]; } set { base["fieldName"] = value; } }
[ConfigurationProperty("fieldId", IsRequired = true)]
public string FieldId { get { return (string)base["fieldId"]; } set { base["fieldId"] = value; } }
}
DeviceFieldonfig d = (DeviceFieldonfig)System.Configuration.ConfigurationManager.GetSection("deviceFieldConfig");
string content = d.Fileds["BCSP2D"];