ExampleConfigurationSectionHandler.cs

 using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization; namespace SampleConfigSectionHandle
{
public sealed class ExampleConfigurationSectionHandler : IConfigurationSectionHandler
{
public object Create(object parent, object configContext, XmlNode section)
{
var xmlSerializer = new XmlSerializer(typeof(UserInfo)); using (var stream = new MemoryStream(Encoding.Default.GetBytes(section.InnerXml)))
{
var xmlNode = XmlReader.Create(stream); return xmlSerializer.Deserialize(xmlNode);
}
}
} public class UserInfo
{
public string UserName { get; set; } public string UserPwd { get; set; }
}
}

App.config

 <?xml version="1.0" encoding="utf-8" ?>
<configuration> <configSections> <!--示例配置节-->
<section name="example" type="SampleConfigSectionHandle.ExampleConfigurationSectionHandler,SampleConfigSectionHandle"/> </configSections> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup> <!--配置节实例-->
<example> <UserInfo>
<UserName>WangYa</UserName>
<UserPwd>123456</UserPwd>
</UserInfo> </example> </configuration>

Program.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace SampleConfigSectionHandle
{
class Program
{
static void Main(string[] args)
{
var config = ConfigurationManager.GetSection("example") as UserInfo; if (config != null)
{
Console.WriteLine(config.UserName);
Console.WriteLine(config.UserPwd);
} Console.Read();
}
}
}

ExampleConfigurationSectionHandler的更多相关文章

随机推荐

  1. vue2.0实战记录

    1. 初始化项目vue init webpack caseone cd caseonecnpm installcnpm install less less-loader -Dcnpm install ...

  2. 转:iOS绘制一个UIView

    绘制一个UIView 绘制一个UIVIew最灵活的方式就是由它自己完成绘制.实际上你不是绘制一个UIView,你只是子类化了UIView并赋予子类绘制自己的能力.当一个UIVIew需要执行绘图操作的时 ...

  3. jQuery中使用attribute,prop获取,设置input的checked值

    1.prop方法获取.设置checked属性 当input控件checkbox设置了checked属性时,无论checked=”“或 checked=”checked”,$(obj).prop(“ch ...

  4. jdk源码

    Java 8 之默认方法(Default Methods) public interface Player { String getName(); default boolean isMale() { ...

  5. js小记 unicode 编码解析

    var str = "\\u6211\\u662Funicode\\u7F16\\u7801"; // 关于这样的数据转换为中文问题,常用的两种方法. // 1. eval 解析 ...

  6. [NOIP提高&洛谷P1024]一元三次方程求解 题解(二分答案)

    [NOIP提高&洛谷P1024]一元三次方程求解 Description 有形如:ax3+bx2+cx+d=0 这样的一个一元三次方程.给出该方程中各项的系数(a,b,c,d 均为实数),并约 ...

  7. Eclipse配置C++环境

    由于实在不想用(界面太丑,超级强迫症),前段时间JAVA一直用eclipse,感觉这个IDE非常友好,看上去很舒服,下载的时候发现有C++版本,于是折腾了一会儿,谷歌上发现好多教程,但是大部分比较老, ...

  8. numpy多项式拟合

    关于解决使用numpy.ployfit进行多项式拟合的时候请注意数据类型,解决问题的思路就是统一把数据变成浮点型,就可以了.这是numpy里面的一个bug,非常low希望后面改善. # coding: ...

  9. 全网最全JS正则表达式 校验数字

    Js代码 <script type="text/javascript"> function SubmitCk() { var reg = /^([a-zA-Z0-9]+ ...

  10. python基础之命名空间

    前言 命名空间通俗的理解就是对象或变量的作用范围,在python中分为局部命令空间.模块命名空间和build-in全局命名空间. 局部命名空间 局部命名空间即在一个函数或一个类中起作用的变量或引用的字 ...