ConfigurationSection類使用心得
ConfigurationSection類主要是方便我們用於擴展自定義webcongfig中的節點信息。我們可以方便的通過以下方式獲取【自定義節點對象】
【你自定義的對象】 config = (【你自定義的對象】)ConfigurationManager.GetSection("【你自定義的節點名稱,如果是sectiongroup的話,請使用XPATH方式】");
使用自定義節點,可能會涉及到這幾個對象的使用:ConfigurationSection【配置域】、ConfigurationElement【節點】、ConfigurationElementCollection【節點列表】
用法一: 配置如下webconfig自定義信息,註意order和lineItem節點都是允許重復出現的
2<configuration>
3 <configSections>
4 <section name="orders" type="ConsoleTest.OrdersSection, ConsoleTest"/>
5 </configSections>
6 <orders companyID="2001">
7 <order number="100001" amount="222.22">
8 <lineItems warehouseNumber="02">
9 <lineItem number="00-000-001" description="wii"/>
10 </lineItems>
11 </order>
12 <order number="300001" amount="33.33">
13 <lineItems warehouseNumber="99">
14 <lineItem number="00-000-001" description="xbox 360"/>
15 <lineItem number="00-000-003" description="playstation 3"/>
16 </lineItems>
17 </order>
18 </orders>
19</configuration>
下面我們要定義相應的實體對象,該實體對象中會有一個子對象【用來表示節點列表信息】(ConfigurationElementCollection)
public class OrdersSection : ConfigurationSection
{
[ConfigurationProperty("companyID", IsRequired = true)]
public string CompanyID
{
get
{
return (string)base["companyID"];
}
set
{
base["companyID"] = value;
}
}
[ConfigurationProperty("", IsDefaultCollection = true)]
public OrderElementCollection Orders
{
get
{
return (OrderElementCollection)base[""];
}
}
}
接下來我們在看看節點列表對象的定義,其中會包含一個子對象【ConfigurationElementCollection】
public class OrderElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new OrderElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((OrderElement)element).Number;
}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
protected override string ElementName
{
get
{
return "order";
}
}
public OrderElement this[int index]
{
get
{
return (OrderElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
}
那麽我們再看看節點對象的定義
public class OrderElement : ConfigurationElement
{
[ConfigurationProperty("number", IsRequired = true)]
public string Number
{
get
{
return (string)base["number"];
}
set
{
base["number"] = value;
}
}
[ConfigurationProperty("amount", IsRequired = true)]
public double Amount
{
get
{
return (double)base["amount"];
}
set
{
base["amount"] = value;
}
}
[ConfigurationProperty("lineItems", IsDefaultCollection = true)]
public LineItemElementCollection LineItems
{
get
{
return (LineItemElementCollection)base["lineItems"];
}
}
}
另外,還有一個子節點列表對象需要定義,【LineItemElementCollection】(ConfigurationElementCollection)
public class LineItemElementCollection : ConfigurationElementCollection
{
[ConfigurationProperty("warehouseNumber", IsRequired = true)]
public string WarehouseNumber
{
get
{
return (string)base["warehouseNumber"];
}
set
{
base["warehouseNumber"] = value;
}
}
protected override ConfigurationElement CreateNewElement()
{
return new LineItemElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ( (LineItemElement)element ).Number;
}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
protected override string ElementName
{
get
{
return "lineItem";
}
}
public LineItemElement this[int index]
{
get
{
return (LineItemElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
}
當然我們還得再定義一個節點對象【LineItemElement
public class LineItemElement : ConfigurationElement{
[ConfigurationProperty("number", IsKey=true, IsRequired = true)]
public string Number
{
get
{
return (string)base["number"];
}
set
{
base["number"] = value;
}
}
[ConfigurationProperty("description", IsRequired = true)]
public string Description
{
get
{
return (string)base["description"];
}
set
{
base["description"] = value;
}
}
}
】
這樣我們就完成了webconfig節點的自定義和對象的實體化, 我們在使用的使用值需要簡單的代碼就能獲取到相應對象的實體信息;如:
OrdersSection config = (OrdersSection)ConfigurationManager.GetSection("orders");
另一中用法:sectionGroup 配置 。如要配置出如下webconfig信息
<configSections>
<sectionGroup name="mygroup">
<section name="mysection"
type="ConfigSection"
allowDefinition="Everywhere"
allowLocation="true"/>
</sectionGroup>
</configSections>
<mygroup>
<mysection user="用戶" password="密碼">
<element element1="屬性1" element2="屬性2"></element>
</mysection>
</mygroup>
那麽我們看下實體是如何定義的 。
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// ConfigSection 的摘要說明
/// </summary>
public class ConfigSection:ConfigurationSection
{
public ConfigSection()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
[ConfigurationProperty("user",DefaultValue="yanghong",IsRequired=true)]
public string User
{
get { return (string)this["user"]; }
set { this["user"] = value; }
}
[ConfigurationProperty("password",DefaultValue="password",IsRequired=true)]
public string PassWord
{
get { return (string)this["password"]; }
set { this["password"] = value; }
}
[ConfigurationProperty("element")]
public elementinfo Element
{
get { return (elementinfo)this["element"]; }
set {this["element"] = value; }
}
}
上面的實體對象包含一個節點對象信息,我們看下這個對象是如何定義的 。
public class elementinfo : ConfigurationElement
{
public elementinfo() { }
[ConfigurationProperty("element1", DefaultValue = "element1", IsRequired = true)]
public string Element1
{
get { return (string)this["element1"]; }
}
[ConfigurationProperty("element2",DefaultValue="element2",IsRequired=true)]
public string Element2
{
get { return (string)this["element2"]; }
}
}
代碼的調用就相當簡單了 :
ConfigSection config = (ConfigSection)ConfigurationManager.GetSection("mygroup/mysection");
Response.Write("用戶名:"+config.User.ToString() + "密碼:" + config.PassWord.ToString() + "元素屬性:" + config.Element.Element1.ToString() + config.Element.Element2.ToString());
ConfigurationSection類使用心得