码迷,mamicode.com
首页 > Windows程序 > 详细

C# 快捷使用自定义配置节点

时间:2017-06-25 20:48:07      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:values   ble   .config   数据格式   system   点数据   自定义   etag   pps   

  C#除了appSettings和connectionStrings默认配置外还允许用户自定义使用配置。C# 提供3中简单的自定义配置,配置文件如下

  

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="Config1" type="System.Configuration.SingleTagSectionHandler"/>
    <section name="Config2" type="System.Configuration.DictionarySectionHandler"/>
    <section name="Config3" type="System.Configuration.NameValueSectionHandler"/>
  </configSections>
  <Config1 a="1" b="2"/>
  <Config2>
    <add key="a" value="1"/>
    <add key="b" value="2"/>
  </Config2>
  <Config3>
    <add key="a" value="1"/>
    <add key="b" value="2"/>
  </Config3>
</configuration>

  使用这3种中配置要注意:

   1.  configSections节点必须要是配置的第一个节点。

   2. 自定义配置要在configSections内部声明。格式是<section name="配置名" type="类的全路径,dll名"/> 系统的这3种配置dll省略。

   3. 针对以上3种配置格式不能变,不能变,不能变,重要的事说3遍 。<Config1 a="1" b="2"></Config1 > 这种写法 抛出异常。

  这3中使用也比较简单,用ConfigurationManager.GetSection(XXX)获取节点数据。第一种和第二种都返回Hashtable类型(Hashtable实现IDictionary接口),第三种返回NameValueCollection 与appSettings返回类型相同。

  

技术分享
 1  public static void Main(string[] args)
 2         {
 3             //    Hashtable
 4             Hashtable config1 = (Hashtable)ConfigurationManager.GetSection("Config1");
 5             Dictionary<string, string> c = new Dictionary<string, string>();
 6             Console.WriteLine("****************配置1**********************");
 7             Console.WriteLine("遍历");
 8             foreach (DictionaryEntry g in config1)
 9             {
10                 Console.WriteLine(g.Key + "=" + g.Value);
11             }
12             Console.WriteLine("使用:a=" + config1["a"]);
13 
14 
15             Hashtable config2 = (Hashtable)ConfigurationManager.GetSection("Config2");
16             Console.WriteLine("****************配置2**********************");
17             Console.WriteLine("遍历");
18             foreach (DictionaryEntry g in config1)
19             {
20                 Console.WriteLine(g.Key + "=" + g.Value);
21             }
22             Console.WriteLine("使用:a=" + config1["a"]);
23 
24 
25             
26             Console.WriteLine("****************配置3**********************");
27             NameValueCollection config3 = (NameValueCollection)ConfigurationManager.GetSection("Config3");
28             Console.WriteLine("遍历");
29             foreach (var item in config3.AllKeys)
30             {
31                 Console.WriteLine(item + "=" + config3[item]);
32             }
33             Console.WriteLine("使用:a=" + config3["a"]);
34 
35             Console.ReadLine();
36         }
获取配置

 调试结果:技术分享

  接下来我们来讲讲如何使用自己格式的配置方法,

  一.建立一个配置文件 如下

  

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <configuration>
 3   <configSections>
 4     <section name="MyConfig" type="CustomConfig.MyConfig.ConfigHandler,CustomConfig" />
 5   </configSections>
 6   
 7   <MyConfig c="3">
 8     <a>1</a>
 9     <b>2</b>
10   </MyConfig>
11   
12     
13 </configuration>

  确定数据格式和解析类。

  二.解析类

  

 1 namespace CustomConfig.MyConfig
 2 {
 3     public class ConfigHandler : IConfigurationSectionHandler
 4     {
 5         public object Create(object parent, object configContext, XmlNode section)
 6         {
 7             ConfigModel model = new ConfigModel //根据需要的类型来返回
 8             {
 9                 a = section.SelectSingleNode("/MyConfig/a").InnerText,
10                 b = section.SelectSingleNode("/MyConfig/b").InnerText,
11                 c = section.SelectSingleNode("/MyConfig").Attributes.GetNamedItem("c").Value
12             };
13             return model;
14         }
15     }
16 
17     public class ConfigModel
18     {
19         public string a { get; set; }
20         public string b { get; set; }
21         public string c { get; set; }
22     }
23 }

  1.配置解析类要实现 IConfigurationSectionHandler j接口  Create函数为具体解析函数,XmlNode section 包含节点全部数据

  2.确定数据的返回格式如 ConfigModel,可根据需要编写。

  3.解析方法和解析Xml相同。

   三. 取配置数据

  方法与之前相同一样用ConfigurationManager.GetSection(XXX)函数 ,执行时会自动调用解析类。

  

public static void Main(string[] args)
        {
            ConfigModel config = (ConfigModel)ConfigurationManager.GetSection("MyConfig");
            Console.WriteLine(config.a + "," + config.b + "," + config.c);
        }

 

  

  

C# 快捷使用自定义配置节点

标签:values   ble   .config   数据格式   system   点数据   自定义   etag   pps   

原文地址:http://www.cnblogs.com/linxingxunyan/p/7077824.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!