.NET 配置文件自定义
来源:互联网 发布:国税局在线发票软件 编辑:程序博客网 时间:2024/05/17 03:15
代码:
/// <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;
0 0
- .NET 配置文件自定义
- 在.NET中自定义配置文件
- 在.NET中自定义配置文件
- .net中自定义配置文件节点
- 在.NET中自定义配置文件(转)
- .NET自定义应用程序配置文件说明app.config
- Asp.net配置文件中自定义节点详解
- 革新:.NET 2.0的自定义配置文件体系初探
- .net应用程序配置文件——自定义配置节
- 革新:.NET 2.0的自定义配置文件体系初探
- 自定义配置文件
- .NET配置文件
- .Net配置文件
- .NET配置文件
- .Net 配置文件
- .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点
- THINKPHP配置文件外【自定义配置文件】
- .NET 插件系统框架设计(二) 使用对象序列化实现自定义配置文件管理
- HDU5723 2016 Multi-University Training Contest 1 (最小生成树+dfs)
- [汇编]伪指令(二)
- uc/os-iii学习笔记---存储管理(内存管理)
- Socket的Send,Recv的长度问题
- 面试知识
- .NET 配置文件自定义
- codeforces#363 C. Vacations dp做法
- iOS(xcode) 加入openssl的方法
- 2016暑期集训---搜索(简单BFS)
- Java多线程-线程的同步与锁
- HDU 5726-GCD
- datetime模块
- HDU 5727 Necklace
- hdoj1722Cake