简单键值对


web.config



                                                                                                                            
   

users.config


  
  


c#

NameValueCollection users = System.Configuration.ConfigurationManager.GetSection("users") as NameValueCollection;
            Response.Write(users.Keys[0]+"
"+users.Keys[1]);


复杂类型

web.config



                                                                         

roles.config


  
  


RolesCofig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EBuy.Chapter3.NTier.WebUI
{
    public class RolesConfig : System.Configuration.IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            return section;
        }
    }
}



c#

XmlNode roles= System.Configuration.ConfigurationManager.GetSection("roles") as XmlNode;
           Response.Write(roles.ChildNodes [0].Attributes["username"].InnerText);

还可以将配置节定义为一个实体

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EBuy.Chapter3.NTier.WebUI
{
    public class RolesConfig : System.Configuration.IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            var list=new List();
            for(int i=0;i 
  
var roles = System.Configuration.ConfigurationManager.GetSection("roles") as List;
          Response.Write(roles.First ().Username);