C# config配置文件 读取

<?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>
      <!--2D 水平补偿-->
      <add fieldName="BCSP2D" fieldId="10032"/>
      <!--2D 垂直补偿-->
      <add fieldName="BCCZ2D" fieldId="10033"/>
      <!--2D 倾角补偿-->
      <add fieldName="BCQJ2D" fieldId="10034"/>
      <!--2D 外轨超高-->
      <add fieldName ="BCWG2D" fieldId="10035"/>
      <!--检测时间-->
      <add fieldName ="JCJCSJ" fieldId="10065"/>
    </fields>
  </deviceFieldConfig>

</configuration>
//使用自定义节点,可能会涉及到这几个对象的使用:
    //ConfigurationSection【配置域】、
    //    ConfigurationElement【节点】、
    //    ConfigurationElementCollection【节点列表】
    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 override ConfigurationElementCollectionType CollectionType
        //{
        //    get
        //    {
        //        return ConfigurationElementCollectionType.BasicMap;
        //    }
        //}

        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
    {
        /// <summary>
        /// IsRequired  获取或设置一个值,指示经过修饰的元素属性是否是必需的
        /// IsKey 如果此属性是该集合中元素的 Key 属性,则为 true;否则为 false。 默认值为 false。
        /// </summary>
        [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"];
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值