第一种Configuration Sections

1.App.config

2.CustomConfigurationManager.cs

3.TestProgram.cs.

App.config

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3. <configSections>
  4. <section name="CustomSettings" type="CustomConfigurationSection.CustomSettingsConfigurationSection,CustomConfigurationSection"/>
  5. </configSections>
  6. <appSettings></appSettings>
  7. <CustomSettings>
  8. <SElement name="name01" value="value01" />
  9. <MElement name="mElement">
  10. <SElement01 name="name02" value="value02" />
  11. <SElement02 name="name03" value="value03" />
  12. </MElement>
  13. <CElement>
  14. <add key="key01" value="value04" />
  15. <add key="key02" value="value05" />
  16. </CElement>
  17. <NElement>
  18. <add key="key03" value="value06">
  19. <SElement03 name="name04" value="value07" />
  20. </add>
  21. <add key="key04" value="value08">
  22. <SElement03 name="name05" value="value09" />
  23. </add>
  24. </NElement>
  25. <M2Element>
  26. <SElement name="name06" value="value10" />
  27. <SElement name="name07" value="value11" />
  28. </M2Element>
  29. </CustomSettings>
  30. </configuration>

CustomConfigurationManager.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Configuration;
  7. using System.Xml;
  8. using System.Xml.XPath;
  9. using System.Xml.Serialization;
  10.  
  11. namespace CustomConfigurationSection
  12. {
  13. public class CustomSettingsConfigurationSection : ConfigurationSection
  14. {
  15. [ConfigurationProperty("SElement")]
  16. public SElement SElement
  17. {
  18. get { return (SElement)this["SElement"]; }
  19. }
  20.  
  21. [ConfigurationProperty("MElement")]
  22. public MElement MElement
  23. {
  24. get { return (MElement)this["MElement"]; }
  25. }
  26.  
  27. [ConfigurationProperty("CElement")]
  28. public TElementCollection CElement
  29. {
  30. get { return (TElementCollection)this["CElement"]; }
  31. }
  32.  
  33. [ConfigurationProperty("NElement")]
  34. public NElementCollection NElement
  35. {
  36. get { return (NElementCollection)this["NElement"]; }
  37. }
  38.  
  39. [ConfigurationProperty("M2Element")]
  40. [ConfigurationCollection(typeof(SElement),AddItemName="SElement")]
  41. public SElementCollection M2Element
  42. {
  43. get { return (SElementCollection)this["M2Element"]; }
  44. }
  45. }
  46.  
  47. public class SElement : ConfigurationElement
  48. {
  49. [ConfigurationProperty("name")]
  50. public String Name
  51. {
  52. get
  53. {
  54. return (String)this["name"];
  55. }
  56. set
  57. {
  58. this["name"] = value;
  59. }
  60. }
  61. [ConfigurationProperty("value")]
  62. public String Value
  63. {
  64. get
  65. {
  66. return (String)this["value"];
  67. }
  68. set
  69. {
  70. this["value"] = value;
  71. }
  72. }
  73. }
  74.  
  75. public class MElement : ConfigurationElement
  76. {
  77. [ConfigurationProperty("name")]
  78. public string Name
  79. {
  80. get { return (string)this["name"]; }
  81. }
  82.  
  83. [ConfigurationProperty("SElement01")]
  84. public SElement SElement01
  85. {
  86. get { return (SElement)this["SElement01"]; }
  87. }
  88. [ConfigurationProperty("SElement02")]
  89. public SElement SElement02
  90. {
  91. get { return (SElement)this["SElement02"]; }
  92. }
  93. }
  94.  
  95. public class TElement : ConfigurationElement
  96. {
  97. [ConfigurationProperty("key")]
  98. public String Key
  99. {
  100. get
  101. {
  102. return (String)this["key"];
  103. }
  104. set
  105. {
  106. this["key"] = value;
  107. }
  108. }
  109.  
  110. [ConfigurationProperty("value")]
  111. public String Value
  112. {
  113. get
  114. {
  115. return (String)this["value"];
  116. }
  117. set
  118. {
  119. this["value"] = value;
  120. }
  121. }
  122. }
  123.  
  124. public class NElement : ConfigurationElement
  125. {
  126. [ConfigurationProperty("key")]
  127. public String Key
  128. {
  129. get
  130. {
  131. return (String)this["key"];
  132. }
  133. set
  134. {
  135. this["key"] = value;
  136. }
  137. }
  138.  
  139. [ConfigurationProperty("value")]
  140. public String Value
  141. {
  142. get
  143. {
  144. return (String)this["value"];
  145. }
  146. set
  147. {
  148. this["value"] = value;
  149. }
  150. }
  151.  
  152. [ConfigurationProperty("SElement03")]
  153. public SElement NestedElement
  154. {
  155. get { return (SElement)this["SElement03"]; }
  156. }
  157. }
  158.  
  159. public class TElementCollection : ConfigurationElementCollection
  160. {
  161. protected override ConfigurationElement CreateNewElement()
  162. {
  163. return new TElement();
  164. }
  165.  
  166. protected override object GetElementKey(ConfigurationElement item)
  167. {
  168. TElement element = (TElement)item;
  169. return getKey(element);
  170. }
  171.  
  172. public TElement this[int index]
  173. {
  174. get
  175. {
  176. return (TElement)BaseGet(index);
  177. }
  178. set
  179. {
  180. if (BaseGet(index) != null)
  181. {
  182. BaseRemove(index);
  183. }
  184. BaseAdd(index, value);
  185. }
  186. }
  187.  
  188. public new TElement this[string name]
  189. {
  190. get
  191. {
  192. return (TElement)BaseGet(name);
  193. }
  194. }
  195.  
  196. public new int Count
  197. {
  198. get { return base.Count; }
  199. }
  200.  
  201. public int IndexOf(TElement item)
  202. {
  203. return BaseIndexOf(item);
  204. }
  205.  
  206. public void RemoveAt(int index)
  207. {
  208. BaseRemoveAt(index);
  209. }
  210.  
  211. public void Add(TElement item)
  212. {
  213. BaseAdd(item);
  214. }
  215.  
  216. public void Clear()
  217. {
  218. BaseClear();
  219. }
  220.  
  221. public bool Contains(TElement item)
  222. {
  223. return BaseIndexOf(item) >= ;
  224. }
  225.  
  226. public void CopyTo(TElement[] array, int arrayIndex)
  227. {
  228. base.CopyTo(array, arrayIndex);
  229. }
  230.  
  231. public new bool IsReadOnly
  232. {
  233. get { return false; }
  234. }
  235.  
  236. public bool Remove(TElement item)
  237. {
  238. if (BaseIndexOf(item) >= )
  239. {
  240. BaseRemove(item);
  241. return true;
  242. }
  243. return false;
  244. }
  245.  
  246. private string getKey(TElement item)
  247. {
  248. return item.Key;
  249. }
  250. }
  251.  
  252. public class NElementCollection : ConfigurationElementCollection
  253. {
  254. protected override ConfigurationElement CreateNewElement()
  255. {
  256. return new NElement();
  257. }
  258.  
  259. protected override object GetElementKey(ConfigurationElement item)
  260. {
  261. NElement element = (NElement)item;
  262. return getKey(element);
  263. }
  264.  
  265. public NElement this[int index]
  266. {
  267. get
  268. {
  269. return (NElement)BaseGet(index);
  270. }
  271. set
  272. {
  273. if (BaseGet(index) != null)
  274. {
  275. BaseRemove(index);
  276. }
  277. BaseAdd(index, value);
  278. }
  279. }
  280.  
  281. public new NElement this[string name]
  282. {
  283. get
  284. {
  285. return (NElement)BaseGet(name);
  286. }
  287. }
  288.  
  289. public new int Count
  290. {
  291. get { return base.Count; }
  292. }
  293.  
  294. public int IndexOf(NElement item)
  295. {
  296. return BaseIndexOf(item);
  297. }
  298.  
  299. public void RemoveAt(int index)
  300. {
  301. BaseRemoveAt(index);
  302. }
  303.  
  304. public void Add(NElement item)
  305. {
  306. BaseAdd(item);
  307. }
  308.  
  309. public void Clear()
  310. {
  311. BaseClear();
  312. }
  313.  
  314. public bool Contains(NElement item)
  315. {
  316. return BaseIndexOf(item) >= ;
  317. }
  318.  
  319. public void CopyTo(NElement[] array, int arrayIndex)
  320. {
  321. base.CopyTo(array, arrayIndex);
  322. }
  323.  
  324. public new bool IsReadOnly
  325. {
  326. get { return false; }
  327. }
  328.  
  329. public bool Remove(NElement item)
  330. {
  331. if (BaseIndexOf(item) >= )
  332. {
  333. BaseRemove(item);
  334. return true;
  335. }
  336. return false;
  337. }
  338.  
  339. private string getKey(NElement item)
  340. {
  341. return item.Key;
  342. }
  343. }
  344.  
  345. public class SElementCollection : ConfigurationElementCollection
  346. {
  347. protected override ConfigurationElement CreateNewElement()
  348. {
  349. return new SElement();
  350. }
  351.  
  352. protected override object GetElementKey(ConfigurationElement item)
  353. {
  354. SElement element = (SElement)item;
  355. return getKey(element);
  356. }
  357.  
  358. public SElement this[int index]
  359. {
  360. get
  361. {
  362. return (SElement)BaseGet(index);
  363. }
  364. set
  365. {
  366. if (BaseGet(index) != null)
  367. {
  368. BaseRemove(index);
  369. }
  370. BaseAdd(index, value);
  371. }
  372. }
  373.  
  374. public new SElement this[string name]
  375. {
  376. get
  377. {
  378. return (SElement)BaseGet(name);
  379. }
  380. }
  381.  
  382. public new int Count
  383. {
  384. get { return base.Count; }
  385. }
  386.  
  387. public int IndexOf(SElement item)
  388. {
  389. return BaseIndexOf(item);
  390. }
  391.  
  392. public void RemoveAt(int index)
  393. {
  394. BaseRemoveAt(index);
  395. }
  396.  
  397. public void Add(SElement item)
  398. {
  399. BaseAdd(item);
  400. }
  401.  
  402. public void Clear()
  403. {
  404. BaseClear();
  405. }
  406.  
  407. public bool Contains(SElement item)
  408. {
  409. return BaseIndexOf(item) >= ;
  410. }
  411.  
  412. public void CopyTo(SElement[] array, int arrayIndex)
  413. {
  414. base.CopyTo(array, arrayIndex);
  415. }
  416.  
  417. public new bool IsReadOnly
  418. {
  419. get { return false; }
  420. }
  421.  
  422. public bool Remove(SElement item)
  423. {
  424. if (BaseIndexOf(item) >= )
  425. {
  426. BaseRemove(item);
  427. return true;
  428. }
  429. return false;
  430. }
  431.  
  432. private string getKey(SElement item)
  433. {
  434. return item.Name;
  435. }
  436. }
  437.  
  438. }

TestProgram.cs

  1. using System;
  2. using System.Collections;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Xml;
  6. using System.Xml.Linq;
  7. using System.Xml.Serialization;
  8. using System.Xml.XPath;
  9. using System.Configuration;
  10.  
  11. namespace CustomConfigurationSection
  12. {
  13. public class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. try
  18. {
  19. CustomSettingsConfigurationSection config = (CustomSettingsConfigurationSection)System.Configuration.ConfigurationManager.GetSection("CustomSettings");
  20. }
  21. catch (Exception ex)
  22. {
  23. System.Diagnostics.Debug.WriteLine(ex.Message);
  24. }
  25. }
  26. }
  27. }

http://blog.csdn.net/zpx3243/article/details/5970585

Custom Configuration 的两种方法:1.Configuration Sections的更多相关文章

  1. Custom Configuration 的两种方法:2.XmlSerializer XmlAttribute

    第二种:XmlSerializer XmlAttribute 1.CustomConfiguration.xml 2.CustomConfigurationSetting.cs 3.CustomCon ...

  2. SpringBoot java配置类@Configuration 的两种写法

    首先在Springboot项目中,件一个java类,使用注解@Configuration  ,则这个类是SpringBoot bean的创建的配置文件类,,这种配置文件类有两种写法 1.使用包扫描 , ...

  3. C# web api返回类型设置为json的两种方法

    web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面就介绍两种方法: 方法一:(改配置法) 找到Global.asax文件,在Applic ...

  4. C# web api 返回类型设置为json的两种方法

    每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默.我眼中的程序员大多都不 ...

  5. Linux安装MySQL的两种方法

    转载:http://blog.csdn.net/superchanon/article/details/8546254/ 1.       运行平台:CentOS 6.3 x86_64,基本等同于RH ...

  6. hive权威安装出现的不解错误!(完美解决)两种方法都可以

    以下两种方法都可以,推荐用方法一! 方法一: 步骤一: yum -y install mysql-server 步骤二:service mysqld start 步骤三:mysql -u root - ...

  7. Cisco设备IOS的恢复方法 两种方法

    如果不小心把Router或者Switch的IOS删除了,特别是Flash中的IOS和ROM中的Mini IOS都没有了的话,连启动都不行的话,有什么方法恢复它呢?答案是方法不只一种,而是两种.其实是我 ...

  8. Hadoop基础-HDFS递归列出文件系统-FileStatus与listFiles两种方法

    Hadoop基础-HDFS递归列出文件系统-FileStatus与listFiles两种方法 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. fs.listFiles方法,返回Loc ...

  9. ToStringBuilder学习(二):两种方法用法优缺点及一个问题

    研究ApacheCommon源码, 先从一个最简单的开始,即围绕Object类里的toString方法自动化实现的一系列类.         怎么来自动化地实现toString方法, 有两种:反射和手 ...

随机推荐

  1. vi编辑器中删除文件中所有字符

    在命令模式下,将光标移动到文档最上方(使用gg命令),然后输入dG,删除工作区内所有缓存数据. 如果想要删除某行文档以下的内容,将光标移动到文档相应行,然后输入dG即可.

  2. MMU功能解析、深入剖析、配置与使用

    MMU = memory management unit 1.把虚拟地址转化成物理地址,防止地址冲突 2.访问权限管理 MMU把一个虚拟地址的20位到31位作为取出来,建立 一张表,叫做transla ...

  3. HQL实现模糊查询

    hibernate 实现模糊查询两种传参方式,其实各个方法的实质都是一样的,只不过传递参数的方法稍微有点区别  public List<User> getUsers(String id){ ...

  4. RPM包搭建

    打包rpm软件包之spec文件解析 1. 概述 RPM的全称是(Red Hat Package Manager,Red Hat包管理器).RPM是一个开放的软件包管理器,工作在Red Hat.类Lin ...

  5. 2019 年百度之星·程序设计大赛 - 初赛一 C. HDU 6670 Mindis 离散化+dijkstra

    题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6670 Mindis Time Limit: 4000/2000 MS (Java/Others) M ...

  6. ZenCart通过Contact Us接收垃圾邮件的过滤方案

    最近收到一些通过Contact Us进行垃圾外链群发的邮件,虽然可以通过在Contact Us增加验证码来解决,但不利于客户体验.所以我们可以通过简单的关键词过滤来实现,一般垃圾外链都含有“[url= ...

  7. UVA - 1649 Binomial coefficients (组合数+二分)

    题意:求使得C(n,k)=m的所有的n,k 根据杨辉三角可以看出,当k固定时,C(n,k)是相对于n递增的:当n固定且k<=n/2时,C(n,k)是相对于k递增的,因此可以枚举其中的一个,然后二 ...

  8. 【鸽】poj3311 Hie with the Pie[状压DP+Floyd]

    题解网上一搜一大坨的,不用复述了吧. 只是觉得网上dp方程没多大问题,但是状态的表示含义模糊.不同于正常哈密顿路径求解,状态表示应当改一下. 首先定义一次移动为从一个点经过若干个点到达另一个点,则$f ...

  9. layui的数据表格加上操作

    数据表格加上操作. <script type="text/html" id="barDemo"> <a class="layui-b ...

  10. pyhton函数

    函数编写文档 放在函数开头的字符串称为文档字符串(docstring),将作为函数的一部分存储起来 def square(x): 'Calculates the square of the numbe ...