.Net 通用配置文件读取方法
去年在window service 插件服务插件开发时,曾经写过一篇.net 中读取自定义Config文件,那个配置文件通用读取方法,只适用于读取键值对,如下面示例所示:
展开
<configuration> <configSections> <sectionGroup name="WeiboClientSectionGroup"> <section name="SinaSection" type="System.Configuration.NameValueSectionHandler,System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <section name="QQSection" type="System.Configuration.NameValueSectionHandler,System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <section name="SohuSection" type="System.Configuration.NameValueSectionHandler,System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <section name="NetEaseSection" type="System.Configuration.NameValueSectionHandler,System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> </sectionGroup> <sectionGroup name="WeiboSectionGroup"> <section name="SinaSection" type="System.Configuration.NameValueSectionHandler,System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <section name="QQSection" type="System.Configuration.NameValueSectionHandler,System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <section name="SohuSection" type="System.Configuration.NameValueSectionHandler,System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <section name="NetEaseSection" type="System.Configuration.NameValueSectionHandler,System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> </sectionGroup> </configSections> <WeiboClientSectionGroup> <SinaSection> <add key="AccessToken" value=""/> <add key="AccessTokenSecret" value=""/> <add key="ResultFormat" value="Json"/> </SinaSection> <!--<QQSection> <add key="AccessToken" value=""/> <add key="AccessTokenSecret" value=""/> <add key="ResultFormat" value="Json"/> </QQSection>--> <QQSection> <add key="AccessToken" value=""/> <add key="AccessTokenSecret" value=""/> <add key="ResultFormat" value="Json"/> </QQSection> <SohuSection> <add key="AccessToken" value=""/> <add key="AccessTokenSecret" value=""/> <add key="ResultFormat" value="Json"/> </SohuSection> <NetEaseSection> <add key="AccessToken" value=""/> <add key="AccessTokenSecret" value=""/> <add key="ResultFormat" value="Json"/> </NetEaseSection> </WeiboClientSectionGroup> <WeiboSectionGroup> <SinaSection> <add key="AppKey" value=""/> <add key="AppSecret" value=""/> <add key="AuthorizeUri" value="http://api.t.sina.com.cn/oauth/authorize"/> <add key="RequestTokenUri" value="http://api.t.sina.com.cn/oauth/request_token"/> <add key="AccessTokenUri" value="http://api.t.sina.com.cn/oauth/access_token"/> <add key="CallBackUri" value="null"></add> </SinaSection> <QQSection> <add key="AppKey" value=""/> <add key="AppSecret" value=""/> <add key="AuthorizeUri" value="https://open.t.qq.com/cgi-bin/authorize"/> <add key="RequestTokenUri" value="https://open.t.qq.com/cgi-bin/request_token"/> <add key="AccessTokenUri" value="https://open.t.qq.com/cgi-bin/access_token"/> <add key="CallBackUri" value="null"></add> </QQSection> <SohuSection> <add key="AppKey" value=""/> <add key="AppSecret" value=""/> <add key="AuthorizeUri" value="http://api.t.sohu.com/oauth/authorize"/> <add key="RequestTokenUri" value="http://api.t.sohu.com/oauth/request_token"/> <add key="AccessTokenUri" value="http://api.t.sohu.com/oauth/access_token"/> <add key="CallBackUri" value="null"></add> </SohuSection> <NetEaseSection> <add key="AppKey" value=""/> <add key="AppSecret" value=""/> <add key="AuthorizeUri" value="http://api.t.163.com/oauth/authenticate"/> <add key="RequestTokenUri" value="http://api.t.163.com/oauth/request_token"/> <add key="AccessTokenUri" value="http://api.t.163.com/oauth/access_token"/> <add key="CallBackUri" value="null"></add> </NetEaseSection> </WeiboSectionGroup> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration>
这种读取方式不灵活,如果遇到复杂的数据结构就无法支持了,下面提供一个自定义处理器的方法达到通用配置的目的。
如有下面这样的配置文件:
展开
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="HtmlEntities" type="Ben.NetUtility.Web.UserControlToHtml.HtmlConfigSection,Ben.NetUtility"/> </configSections> <HtmlEntities> <HtmlEntity> <Name></Name> <Url></Url> <SecurityIdentification></SecurityIdentification> </HtmlEntity> </HtmlEntities> </configuration>
只要需要三个步骤就可以完成读取上面的配置文件:
1、定义一个从ConfigEntityBase<T>继承的实体,如下所示:
展开
public class HtmlEntity : ConfigEntityBase<HtmlEntity> { public string Name { get; private set; } public string Url { get; private set; } public List<string> SecurityIdentification { get; private set; } public HtmlEntity() { SecurityIdentification = new List<string>(); } public override HtmlEntity Parse(XmlNode node) { var entity = new HtmlEntity(); foreach (XmlNode childNode in node.ChildNodes) { ..//解析对象 } return entity; } }
2、定义一个Handler处理器,如下代码所示:
public class HtmlConfigSection:Config.ConfigurationSectionHandlerHelper<HtmlEntity> { }
3、定义读取配置工具类:
展开
public class HtmlConfig:Config.ConfigBase<List<HtmlEntity>> { public HtmlConfig():base("HtmlEntities",null){} public HtmlEntity this[string name] { get { return Section.FirstOrDefault(p => p.Name == name); } } }
通过上面的三个步骤,就完成了配置的读取,使用方法如下:
展开
var ConfigContent = new HtmlConfig();
基础类库下载:Config.rar