C# config配置文件 读取

xml 复制代码
<?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>
csharp 复制代码
//使用自定义节点,可能会涉及到这几个对象的使用:
    //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; } }

    }
csharp 复制代码
 DeviceFieldonfig d = (DeviceFieldonfig)System.Configuration.ConfigurationManager.GetSection("deviceFieldConfig");
            string content = d.Fileds["BCSP2D"];
相关推荐
冰茶_30 分钟前
WPF之TextBlock控件详解
microsoft·微软·c#·wpf·wpf控件
学习中的码虫1 小时前
c#栈及其应用
开发语言·c#
CHQIUU4 小时前
C# 中重启程序通常意味着关闭当前运行的应用程序实例
开发语言·c#
oMMh6 小时前
使用C# ASP.NET创建一个可以由服务端推送信息至客户端的WEB应用(1)
前端·c#·asp.net
冰茶_6 小时前
WPF之Button控件详解
大数据·学习·microsoft·c#·wpf
且听风吟ayan6 小时前
leetcode day37 474
leetcode·c#
大飞pkz7 小时前
【Unity】MVC的简单分享以及一个在UI中使用的例子
unity·c#·mvc·框架·ui框架·商业级ui框架
DanmF--8 小时前
详解UnityWebRequest类
网络·unity·c#·游戏引擎·游戏程序
爱编程的鱼9 小时前
C# 继承详解
开发语言·c#
神仙别闹10 小时前
基于C#窗体+GDI+绘图实现分形树
开发语言·c#