代码:
/// <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;