.NET 配置文件自定义

 代码:       
/// <summary>
    /// 企业信息配置节
    /// </summary>
    public class CompanySetting : ConfigurationSection
    {
        [ConfigurationProperty("companylist", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(CompanyInfoCollection),
            AddItemName = "add",
            ClearItemsName = "clear",
            RemoveItemName = "remove")]
        public CompanyInfoCollection CompanyList
        {
            get
            {
                CompanyInfoCollection urlsCollection = (CompanyInfoCollection)base["companylist"];
                return urlsCollection;
            }
        }
    }
 
    public class CompanyInfoCollection : ConfigurationElementCollection
    {
        public CompanyInfoCollection()
        {
            CompanyInfoElement companyInfo = (CompanyInfoElement)CreateNewElement();
            Add(companyInfo);
        }
 
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }
 
        protected override ConfigurationElement CreateNewElement()
        {
            return new CompanyInfoElement();
        }
 
        protected override Object GetElementKey(ConfigurationElement element)
        {
            return ((CompanyInfoElement)element).UserName;
        }
 
        public CompanyInfoElement this[int index]
        {
            get
            {
                return (CompanyInfoElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }
 
        new public CompanyInfoElement this[string userName]
        {
            get
            {
                return (CompanyInfoElement)BaseGet(userName);
            }
        }
 
        public int IndexOf(CompanyInfoElement userName)
        {
            return BaseIndexOf(userName);
        }
 
        public void Add(CompanyInfoElement userName)
        {
            BaseAdd(userName);
        }
        protected override void BaseAdd(ConfigurationElement element)
        {
            BaseAdd(element, false);
        }
 
        public void Remove(CompanyInfoElement url)
        {
            if (BaseIndexOf(url) >= 0)
                BaseRemove(url.UserName);
        }
 
        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }
 
        public void Remove(string userName)
        {
            BaseRemove(userName);
        }
 
        public void Clear()
        {
            BaseClear();
        }
    }
 
 
    /// <summary>
    /// 企业信息
    /// </summary>
    public class CompanyInfoElement : ConfigurationElement
    {
        /// <summary>
        /// 企业名称
        /// </summary>
        [ConfigurationProperty("companyname", IsRequired = true)]
        public string CompanyName
        {
            get { return this["companyname"].ToString(); }
            set { this["companyname"= value; }
        }
 
        /// <summary>
        /// 用户名称
        /// </summary>
        [ConfigurationProperty("username", IsRequired = true, IsKey = true)]
        public string UserName
        {
            get { return this["username"].ToString(); }
            set { this["username"= value; }
        }
 
 
        /// <summary>
        /// 接入码
        /// </summary>
        [ConfigurationProperty("passcode", IsRequired = true)]
        public string PassCode
        {
            get { return this["passcode"].ToString(); }
            set { this["passcode"= value; }
        }
 
 
        /// <summary>
        /// 企业名称
        /// </summary>
        [ConfigurationProperty("password", IsRequired = true)]
        public string Password
        {
            get { return this["password"].ToString(); }
            set { this["password"= value; }
        }
 
        /// <summary>
        /// 平台接入码
        /// </summary>
        [ConfigurationProperty("platpasscode", IsRequired = true)]
        public string PlatPassCode
        {
            get { return this["platpasscode"].ToString(); }
            set { this["platpasscode"= value; }
        }
 
        /// <summary>
        /// 平台用户名
        /// </summary>
        [ConfigurationProperty("platusername", IsRequired = true)]
        public string PlatUserName
        {
            get { return this["platusername"].ToString(); }
            set { this["platusername"= value; }
        }
 
        /// <summary>
        /// 平台密码
        /// </summary>
        [ConfigurationProperty("platpassword", IsRequired = true)]
        public string PlatPassword
        {
            get { return this["platpassword"].ToString(); }
            set { this["platpassword"= value; }
        }
 
        /// <summary>
        /// 平台对接IP
        /// </summary>
        [ConfigurationProperty("platip", IsRequired = true)]
        public string PlatIP
        {
            get { return this["platip"].ToString(); }
            set { this["platip"= value; }
        }
 
        /// <summary>
        /// 企业名称
        /// </summary>
        [ConfigurationProperty("platpoint", IsRequired = true)]
        public string PlatPoint
        {
            get { return this["platpoint"].ToString(); }
            set { this["platpoint"= value; }
        }

    }

配置项:

  <!--企业信息设置-->
  <companySetting>
    <!--企业信息-->
    <companylist>
      <add companyname="浚县顺达公交有限公司" passcode="6011016713" username="210FF3_gj" password="151517" platpasscode="212162134" platusername="HNYB1606212534" platpassword="12534" platip="218.28.140.212" platpoint="10088"/>
    </companylist>
  </companySetting>

代码:

                var companySetting = (CompanySetting)ConfigurationManager.GetSection("companySetting");

这个地方为什么会读出是两条记录。
                for (int i = 0; i < companySetting.CompanyList.Count; i++)
                {
                    var userName = companySetting.CompanyList[i].UserName;
                    if (String.IsNullOrEmpty(userName))
                        continue;
                    var password = companySetting.CompanyList[i].Password;
                    var passcode = companySetting.CompanyList[i].PassCode;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值