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

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

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

1.定义实体类Student

Code:
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. namespaceConsoleApplication{
  6. publicenumGender{
  7. Female,
  8. Male
  9. }
  10. publicenumGrade{
  11. G1,
  12. G2,
  13. G3
  14. }
  15. publicclassStudent{
  16. publicstringName{get;set;}
  17. publicintAge{get;set;}
  18. publicGenderGender{get;set;}
  19. publicGradeGrade{get;set;}
  20. publicstringHobby{get;set;}
  21. publicoverridestringToString(){
  22. returnstring.Format("我是{0}{1},今年{2}岁,我喜欢{3}。",
  23. Name,
  24. Gender==Gender.Male?"男生":"女生",
  25. Age,
  26. Hobby);
  27. }
  28. }
  29. }

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

Code:
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingSystem.Configuration;
  6. usingSystem.Xml;
  7. namespaceConsoleApplication{
  8. publicclassStudentSectionHandler:IConfigurationSectionHandler{
  9. publicobjectCreate(objectparent,objectconfigContext,XmlNodesection){
  10. List<Student>stuList=newList<Student>();
  11. try{
  12. foreach(XmlNodestuNodeinsection.ChildNodes){
  13. Studentstu=newStudent();
  14. foreach(XmlNodenodeinstuNode.ChildNodes){
  15. switch(node.Name){
  16. case"Name":stu.Name=node.InnerText;break;
  17. case"Age":stu.Age=Convert.ToInt32(node.InnerText);break;
  18. case"Gender":stu.Gender=(Gender)Enum.Parse(typeof(Gender),node.InnerText);break;
  19. case"Grade":stu.Grade=(Grade)Enum.Parse(typeof(Grade),node.InnerText);break;
  20. case"Hobby":stu.Hobby=node.InnerText;break;
  21. }
  22. }
  23. stuList.Add(stu);
  24. }
  25. }catch(Exceptionex){
  26. throwex;
  27. }
  28. returnstuList;
  29. }
  30. }
  31. }

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

Code:
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <configuration>
  3. <configSections>
  4. <sectionname="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. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingSystem.Configuration;
  6. namespaceConsoleApplication{
  7. classProgram{
  8. staticvoidMain(string[]args){
  9. List<Student>stuList=ConfigurationManager.GetSection("Students")asList<Student>;
  10. if(stuList!=null){
  11. foreach(StudentstuinstuList){
  12. Console.WriteLine(stu);
  13. }
  14. }
  15. }
  16. }
  17. }

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值