最近写了一段自定义的ConfigurationSection继承类,通过该class可以轻松得定义和读取配置文件信息,
注意这里使用的是c# 2.0来实现的,相比1.1必须通过实现IConfigurationSectionHandler接口来自定义配置节点类方便多了
不论是web.config还是app.config,都可以使用ConfigurationManager类加载配置文件中自定义的节点内容。

以下是配置文件的层次结构:

 1通过ConfigurationSection来轻松地加载配置文件<?xml version="1.0" encoding="utf-8" ?>
 2通过ConfigurationSection来轻松地加载配置文件<configuration>
 3通过ConfigurationSection来轻松地加载配置文件  <configSections>
 4通过ConfigurationSection来轻松地加载配置文件    <section name="orders" type="ConsoleTest.OrdersSection, ConsoleTest"/>
 5通过ConfigurationSection来轻松地加载配置文件  </configSections>
 6通过ConfigurationSection来轻松地加载配置文件  <orders companyID="2001">
 7通过ConfigurationSection来轻松地加载配置文件    <order number="100001" amount="222.22">
 8通过ConfigurationSection来轻松地加载配置文件      <lineItems warehouseNumber="02">
 9通过ConfigurationSection来轻松地加载配置文件        <lineItem number="00-000-001" description="wii"/>
10通过ConfigurationSection来轻松地加载配置文件      </lineItems>
11通过ConfigurationSection来轻松地加载配置文件    </order>
12通过ConfigurationSection来轻松地加载配置文件    <order number="300001" amount="33.33">
13通过ConfigurationSection来轻松地加载配置文件      <lineItems warehouseNumber="99">
14通过ConfigurationSection来轻松地加载配置文件        <lineItem number="00-000-001" description="xbox 360"/>
15通过ConfigurationSection来轻松地加载配置文件        <lineItem number="00-000-003" description="playstation 3"/>
16通过ConfigurationSection来轻松地加载配置文件      </lineItems>
17通过ConfigurationSection来轻松地加载配置文件    </order>
18通过ConfigurationSection来轻松地加载配置文件  </orders>
19通过ConfigurationSection来轻松地加载配置文件</configuration>

注意order和lineItem节点都是允许重复出现的

以下是继承自ConfigurationSection的自定义配置节点类:

  1通过ConfigurationSection来轻松地加载配置文件public class OrdersSection : ConfigurationSection
  2    }

相关文章: