C# 自定义配置文件节点操作

本文详细介绍了一种基于XML的配置文件创建与管理方法,包括配置文件结构、组件配置项类、配置集合类及节点类的定义。通过实例演示了如何进行配置项的增删改查,适用于.NET平台下应用程序的配置管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.新建配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="Components" type="Mi.CompoentConfigurationSection,Mi"/>
  </configSections>
  <Components BasePath="~\Libs\Pool\">
    <add DBComponentID="1" DatabaseName="master"  SortNo="1" />
    <add DBComponentID="2" DatabaseName="Student" SortNo="2" />
  </Components>
</configuration>

2.新建组件配置项类

    /// <summary>
    /// 组件配置项
    /// </summary>
    [Serializable]
    public class ComponentElement : ConfigurationElement
    {
 
        [ConfigurationProperty("DBComponentID", IsRequired = true)]
        public int DBComponentID
        {
            get { return Convert.ToInt32(this["DBComponentID"]); }
            set { this["DBComponentID"] = value; }
        }


        [ConfigurationProperty("DatabaseName", IsRequired = true)]
        public string DatabaseName
        {
            get { return this["DatabaseName"].ToString(); }
            set { this["DatabaseName"] = value; }
        }


        [ConfigurationProperty("SortNo", IsRequired = false, DefaultValue = 0)]
        public int SortNo
        {
            get { return Convert.ToInt32(this["SortNo"]); }
            set { this["SortNo"] = value; }
        }
    }

3.新建组件配置集合类

    /// <summary>
    /// 组件配置集合
    /// </summary>
    [ConfigurationCollection(typeof(ComponentElement))]
    public class ComponentCollection : ConfigurationElementCollection
    {
        public ComponentCollection() : base(StringComparer.OrdinalIgnoreCase)
        {

        }
        protected override ConfigurationElement CreateNewElement()
        {
            return new ComponentElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as ComponentElement).DBComponentName;
        }

        new public ComponentElement this[string dbComponentName]
        {
            get
            {
                return (ComponentElement)base.BaseGet(dbComponentName);
            }
        }

        public void Add(ComponentElement item)
        {
            this.BaseAdd(item);
        }

        public void Clear()
        {
            base.BaseClear();
        }

        public void Remove(string databaseName)
        {
            base.BaseRemove(databaseName);
        }
    }

4.新建组件配置节点类

    /// <summary>
    /// 组件配置节点
    /// </summary>
    public class CompoentConfigurationSection : ConfigurationSection
    {
        private static readonly ConfigurationProperty s_property = new ConfigurationProperty(string.Empty, typeof(ComponentCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);

        [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
        public ComponentCollection Components
        {
            get
            {
                return (ComponentCollection)base[s_property];
            }
        }


        [ConfigurationProperty("BasePath", IsRequired = false)]
        public string BasePath
        {
            get
            {
                return ReMapBasePath(this["BasePath"].ToString());
            }
            set
            {
                this["BasePath"] = ReMapBasePath(value);
            }
        }

        private string ReMapBasePath(string basePath)
        {
            if (basePath.Trim().StartsWith("~\\"))
            {
                basePath = basePath.Replace("~\\", AppDomain.CurrentDomain.BaseDirectory + "\\");
            }
            return basePath;
        }
    }

5.使用方式

 

        /// <summary>
        /// 只读组件配置项
        /// </summary>
        public static readonly CompoentConfigurationSection ReadOnlyCompoents =      ConfigurationManager.GetSection("Components") as CompoentConfigurationSection;

 public static readonly Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        /// <summary>
        /// 读写主件配置项
        /// </summary>
        public static  CompoentConfigurationSection ReadWriteCompoents =

configuration.GetSection("Components") as CompoentConfigurationSection;

ReadWriteCompoents .Components.Remove("master");//删除

//读写

  foreach (ComponentElement itemComponent in ReadWriteCompoents .Components)

{

               itemComponent.DatabaseName="MyStudent";

}

configuration.Save(ConfigurationSaveMode.Modified);//只保存修改变动

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值