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[""]; } } } 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"]; } } } 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); } } } 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; } } } 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"]; } } }
<configSections> <section name="orders" type="ConsoleApplication1.OrdersSection, ConsoleApplication1"/> <sectionGroup name="mygroup"> <section name="mysection" type="ConsoleApplication1.ConfigSection, ConsoleApplication1" allowDefinition="Everywhere" allowLocation="true"/> </sectionGroup> </configSections>
<orders companyID="2001"> <order number="100001" amount="222.22"> <lineItems warehouseNumber="02"> <lineItem number="00-000-001" description="wii"/> </lineItems> </order> <order number="300001" amount="33.33"> <lineItems warehouseNumber="99"> <lineItem number="00-000-001" description="xbox 360"/> <lineItem number="00-000-003" description="playstation 3"/> </lineItems> </order> </orders> <mygroup> <mysection user="用户" password="密码"> <element element1="属性1" element2="属性2"></element> </mysection> </mygroup>
OrdersSection section = ConfigurationManager.GetSection("orders") as OrdersSection; ConfigSection config = (ConfigSection)ConfigurationManager.GetSection("mygroup/mysection");
时间: 2024-10-04 17:27:33