App.config文件如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="Companies" type="ConvertCV.ConvertCfgHandler,ConvertCV"/>
</configSections>
<Companies>
<Company Enabled="false">
<ID>563</ID>
<Name>公司AA</Name>
<SourceFolder>E:\test\PubResume\aa</SourceFolder>
<TargetFolder>E:\test\PubResume\aa_cvt</TargetFolder>
</Company>
<Company Enabled="false">
<ID>234</ID>
<Name>公司BB</Name>
<SourceFolder>E:\test\PubResume\bb</SourceFolder>
<TargetFolder>E:\test\PubResume\bb_cvt</TargetFolder>
</Company>
<Company Enabled="true">
<ID>384</ID>
<Name>公司CC</Name>
<SourceFolder>E:\test\PubResume\cc</SourceFolder>
<TargetFolder>E:\test\PubResume\cc_cvt</TargetFolder>
</Company>
</Companies>
<appSettings>
<!--要处理哪天的数据,如果为空,则处理当天的数据-->
<add key="PubDate" value="2009-3-22"/>
<!--基础数据目录-->
<add key="BaseDataPath" value="E:\Batch\test\bin\Debug\BaseData"/>
</appSettings>
</configuration>
读取自定义配置文件:
第一步,先创建configuration section类
public class CompanySection
{
public CompanySection()
{
}
public CompanySection(XmlNode company)
{
XmlElement c = (XmlElement)company;
_enabled = Convert.ToBoolean(c.Attributes["Enabled"].Value);
_id = c.SelectSingleNode("ID").InnerText;
_name = c.SelectSingleNode("Name").InnerText;
_sourceFolder = c.SelectSingleNode("SourceFolder").InnerText;
if (_sourceFolder.Substring(_sourceFolder.Length - 1) != @"\")
_sourceFolder += @"\";
_targetFolder = c.SelectSingleNode("TargetFolder").InnerText;
if (_targetFolder.Substring(_targetFolder.Length - 1) != @"\")
_targetFolder += @"\";
}
#region 属性
private string _id = "";
private string _name = "";
private string _sourceFolder = "";
private string _targetFolder = "";
private bool _enabled = true;
public string ID
{
get { return _id; }
set { _id = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public string SourceFolder
{
get { return _sourceFolder; }
set { _sourceFolder = value; }
}
public string TargetFolder
{
get { return _targetFolder; }
set { _targetFolder = value; }
}
public bool Enabled
{
get { return _enabled; }
set { _enabled = value; }
}
#endregion
}
第二步, 需要继承IConfigurationSectionHandler接口
public class ConvertCfgHandler : IConfigurationSectionHandler
{
public ConvertCfgHandler()
{
}
#region IConfigurationSectionHandler Members
public object Create(object parent, object configContext, XmlNode section)
{
List<CompanySection> _list = new List<CompanySection>();
foreach(XmlNode company in section.SelectNodes("//Company"))
{
CompanySection _company = new CompanySection(company);
if(_company.Enabled)
_list.Add(_company);
}
return _list;
}
#endregion
}
第三步,读取配置项
public class CompanyConfig
{
/// <summary>
/// 获取配置文件中的公司信息
/// </summary>
/// <returns></returns>
public static List<CompanySection> GetCompanyList()
{
return (List<CompanySection>)ConfigurationSettings.GetConfig("Companies");
}
}
第四步,使用配置内容
static void Main()
{
private List<CompanySection> _companyList = CompanyConfig.GetCompanyList();
foreach(CompanySection company in _companyList)
{
//处理代码
}
}