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