在web.config或者app.config中增加自定义配置节


简单键值对


web.config


<configSections>
                                                                                                                            
   <section name="users" type="System.Configuration.NameValueSectionHandler"/>
                                                                                                                             
                                                                                                                             
 </configSections>
 <users  configSource="users.config"></users>

users.config

<users>
  <add key="beijing" value="123"></add>
  <add key="tianjin" value="123"></add>
</users>


c#

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


复杂类型

web.config


<configSections>
                                                                         
<section name="roles" type="EBuy.Chapter3.NTier.WebUI.RolesConfig, EBuy.Chapter3.NTier.WebUI"/>
                                                                         
</configSections>
<roles configSource="roles.config">
  </roles>

roles.config

<roles>
  <add username="beijing" password="123"></add>
  <add username="tianjin" password="123"></add>
</roles>


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<Role>();
            for(int i=0;i<section.ChildNodes.Count;i++)
            {
                list.Add(new Role (){
                    Username =section.ChildNodes[i].Attributes["username"].InnerText ,
                    Password =section.ChildNodes[i].Attributes["password"].InnerText });
            }
            return list;
        }
    }
    public class Role
    {
        public string Username { get; set; }
        public string Password{get;set;}
    }
}
var roles = System.Configuration.ConfigurationManager.GetSection("roles") as List<EBuy.Chapter3.NTier.WebUI.Role >;
          Response.Write(roles.First ().Username);


本文出自 “突破中的IT结构师” 博客,请务必保留此出处http://virusswb.blog.51cto.com/115214/1374335

在web.config或者app.config中增加自定义配置节,布布扣,bubuko.com

在web.config或者app.config中增加自定义配置节

上一篇:[Android]Dalvik的BOOTCLASSPATH和dexopt流程


下一篇:Android appcompat