自定义config文件section操作

给config文件添加自定义section

ContractedBlock.gif ExpandedBlockStart.gif App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  
<configSections>
    
<section name="ProjectSection" type="ConsoleApplication1.ProjectSection, ConsoleApplication1"/>
  
</configSections>

  
<ProjectSection name="ProjectCompany">
    
<role name="administartor"/>
    
<role name="manager"/>
    
<role name="support"/>
  
</ProjectSection>

</configuration>
ContractedBlock.gif ExpandedBlockStart.gif Code
 1using System;
 2using System.Collections.Generic;
 3using System.Configuration;
 4using System.Xml;
 5
 6namespace ConsoleApplication1
 7ExpandedBlockStart.gifContractedBlock.gif{
 8    internal class Program
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    {
10        private static void Main()
11ExpandedSubBlockStart.gifContractedSubBlock.gif        {
12            ProjectSection projectSection = ConfigurationManager.GetSection("ProjectSection"as ProjectSection;
13
14            Console.WriteLine(projectSection.name);
15            foreach (var role in projectSection.roles)
16ExpandedSubBlockStart.gifContractedSubBlock.gif            {
17                Console.WriteLine(role.name);
18            }

19
20            Console.ReadLine();
21        }

22    }

23
24    public class Role
25ExpandedSubBlockStart.gifContractedSubBlock.gif    {
26ExpandedSubBlockStart.gifContractedSubBlock.gif        public string name getset; }
27    }

28
29    public class ProjectSection : IConfigurationSectionHandler
30ExpandedSubBlockStart.gifContractedSubBlock.gif    {
31ExpandedSubBlockStart.gifContractedSubBlock.gif        public Role[] roles getset; }
32
33ExpandedSubBlockStart.gifContractedSubBlock.gif        public string name getset; }
34
35        public object Create(object parent, object configContext, XmlNode section)
36ExpandedSubBlockStart.gifContractedSubBlock.gif        {
37            ProjectSection ps = new ProjectSection();
38            ps.name = section.Attributes["name"].Value;
39            if (parent != null)
40ExpandedSubBlockStart.gifContractedSubBlock.gif            {
41                ps = (ProjectSection)parent;
42            }

43
44            List<Role> list = new List<Role>();
45            foreach (XmlNode node in section.ChildNodes)
46ExpandedSubBlockStart.gifContractedSubBlock.gif            {
47                if (null != node.Attributes)
48ExpandedSubBlockStart.gifContractedSubBlock.gif                {
49                    Role item = new Role();
50                    item.name = node.Attributes["name"].Value;
51                    list.Add(item);
52                }

53            }

54            ps.roles = list.ToArray();
55            return ps;
56        }

57    }

58}

转载于:https://www.cnblogs.com/rickiedu/archive/2009/05/19/1460300.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值