应用程序配置文件中读取自定义配置节

有时候,我们需要在应用程序配置文件(app.config)或网站配置文件(web.config)中自定义一些信息,靠appSetting和connectionString不能满足需求。

首先在配置文件中添加configSections节,说明自定义配置节的名称,并制定读取方式(用什么处理程序来读取),这里我演示自定义处理程序读取,自定义处理程序必须实现IConfigurationSectionHandler接口

1.定义实体类Student

Code:
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Text;   
  5.   
  6. namespace ConsoleApplication {   
  7.   
  8.     public enum Gender {   
  9.         Female,   
  10.         Male   
  11.     }   
  12.   
  13.     public enum Grade{   
  14.         G1,   
  15.         G2,   
  16.         G3   
  17.     }   
  18.   
  19.     public class Student {   
  20.         public string Name { getset; }   
  21.         public int Age { getset; }   
  22.         public Gender Gender { getset; }   
  23.         public Grade Grade { getset; }   
  24.         public string Hobby { getset; }   
  25.   
  26.         public override string ToString() {   
  27.             return string.Format("我是{0}{1},今年{2}岁,我喜欢{3}。",   
  28.                 Name,   
  29.                 Gender==Gender.Male?"男生":"女生",   
  30.                 Age,   
  31.                 Hobby);   
  32.         }   
  33.     }   
  34. }  

2.定义自定义处理程序StudentSectionHandler来创建对象

Code:
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Text;   
  5. using System.Configuration;   
  6. using System.Xml;   
  7.   
  8. namespace ConsoleApplication {   
  9.     public class StudentSectionHandler : IConfigurationSectionHandler {   
  10.         public object Create(object parent, object configContext, XmlNode section) {   
  11.             List<Student> stuList = new List<Student>();   
  12.             try {   
  13.                 foreach(XmlNode stuNode in section.ChildNodes){   
  14.                     Student stu = new Student();   
  15.                     foreach (XmlNode node in stuNode.ChildNodes) {   
  16.                         switch (node.Name) {   
  17.                             case "Name": stu.Name = node.InnerText; break;   
  18.                             case "Age": stu.Age = Convert.ToInt32(node.InnerText); break;   
  19.                             case "Gender": stu.Gender = (Gender)Enum.Parse(typeof(Gender), node.InnerText); break;   
  20.                             case "Grade": stu.Grade = (Grade)Enum.Parse(typeof(Grade), node.InnerText); break;   
  21.                             case "Hobby": stu.Hobby = node.InnerText; break;   
  22.                         }   
  23.                     }   
  24.                     stuList.Add(stu);   
  25.                 }   
  26.             } catch (Exception ex) {   
  27.                 throw ex;   
  28.             }   
  29.             return stuList;   
  30.         }   
  31.     }   
  32. }   

3.配置文件中加入自定义信息

Code:
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.     <configSections>  
  4.         <section name="Students" type="ConsoleApplication.StudentSectionHandler,ConsoleApplication,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" />  
  5.     </configSections>  
  6.     <Students>  
  7.         <Student>  
  8.             <Name>Sonny.Lin</Name>  
  9.             <Age>30</Age>  
  10.             <Gender>Male</Gender>  
  11.             <Grade>G2</Grade>  
  12.             <Hobby>旅游</Hobby>  
  13.         </Student>  
  14.         <Student>  
  15.             <Name>Willim.Lin</Name>  
  16.             <Age>3</Age>  
  17.             <Gender>Male</Gender>  
  18.             <Hobby>玩、看电视</Hobby>  
  19.         </Student>  
  20.     </Students>  
  21. </configuration>  

注意:
1.section中定义了Students(xml)节点,而Students中定义了两个Student,所以这个处理程序应该返回List<Student>类型;
2.处理程序的type一定要用强类型,“完整类名 ,  程序集名 , 版本信息 ,  区域信息 ,  公钥 ”,根据自己的不同名称需要更改;

4.测试程序入口

Code:
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Text;   
  5. using System.Configuration;   
  6.   
  7. namespace ConsoleApplication {   
  8.     class Program {   
  9.         static void Main(string[] args) {   
  10.             List<Student> stuList = ConfigurationManager.GetSection("Students"as List<Student>;   
  11.             if (stuList != null) {   
  12.                 foreach (Student stu in stuList) {   
  13.                     Console.WriteLine(stu);   
  14.                 }   
  15.             }   
  16.         }   
  17.     }   
  18. }  

下载地址:http://u.163.com/VmMfE
提取码:jiqul3zh

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值