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"];
相关推荐
阿蒙Amon8 分钟前
06. C#入门系列【自定义类型】:从青铜到王者的进阶之路
开发语言·c#
钢铁男儿3 小时前
C# 表达式和运算符(表达式和字面量)
开发语言·c#
林鸿群4 小时前
C#子线程更新主线程UI及委托回调使用示例
开发语言·c#
o0向阳而生0o4 小时前
63、.NET 异常处理
c#·.net·异常处理
SteveDraw7 小时前
C++动态链接库封装,供C#/C++ 等编程语言使用——C++动态链接库概述(总)
开发语言·c++·c#·封装·动态链接库
Kookoos7 小时前
性能剖析:在 ABP 框架中集成 MiniProfiler 实现性能可视化诊断
后端·c#·.net·abp vnext·miniprofiler
阿翰9 小时前
自动 GitHub Readme 20 种语言翻译平台 - OpenAiTx 开源免费
c#·.net
枫叶kx13 小时前
1Panel运行的.net程序无法读取系统字体(因为使用了docker)
c#
军训猫猫头18 小时前
96.如何使用C#实现串口发送? C#例子
开发语言·c#
不爱写代码的玉子20 小时前
HALCON透视矩阵
人工智能·深度学习·线性代数·算法·计算机视觉·矩阵·c#