【附】官网例子
在.Net中的System.Configuration
命名空间中为我们app.config
中自定义配置提供了完美的支持。
在配置文件中使用自定义配置,需要在configSections
中添加一个section
元素,并制定此section
元素对应的类型和名字。然后再在configuration
根节点下面添加此自定义配置。
section
标签中name
为自定义标签名称,type
为命名空间+类名,程序集名称- 自定义标签数据:
Film
为自定义标签(ConfigurationSection
),actors
为自定义标签数据集(ConfigurationElementCollention
),director
和add
为数据集里的model
(ConfigurationElement
)。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!--自定义数据结构-->
<section name="Film" type="ConfigExample.ConfigFilm, ConfigExample"/>
</configSections>
<Film fName="wujiandao" year ="2002">
<director firstName="Liu" lastName="weiqiang"/>
<actors>
<add name="Liang chaowei" roleName="chen yongren"/>
<add name="Liu dehua" roleName="liu jianming"/>
</actors>
</Film>
</configuration>
在此配置文件中,配置的名字是Film
,他有包含属性name,year
,节点内还有一个director
的元素包含firstName
和lastName
的属性,节点内还包含了actors
的子节点集合里面定义了一组add
元素包含name
和roleName
两个属性。
为了能够使用自定义配置文件,我们还需要实现存取这个配置块的类,标签分为3部分,代码也对应3个继承类:ConfigurationSection
,ConfigurationElementCollection
,ConfigurationElement
。其中类的属性和标签属性使用:ConfigurationProperty
(“标签属性”)进行对应,需要对get,set方法进行改造。
具体代码如下:
using System.Configuration;
namespace ConfigExample
{
//对应Film节点
public class ConfigFilm : ConfigurationSection
{
//对应Film节点的属性:fName
[ConfigurationProperty("fName")]
public string FName
{
get { return base["fName"].ToString(); }
set { base["fName"] = value; }
}
//对应Film节点的属性:year
[ConfigurationProperty("year")]
public string Year
{
get { return base["year"].ToString(); }
set { base["year"] = value; }
}
//对应Film节点的子元素director
[ConfigurationProperty("director", IsDefaultCollection = false)]
public DirectorSection Director
{
get { return (DirectorSection)base["director"]; }
set { base["director"] = value; }
}
//对应集合类属性actors
[ConfigurationProperty("actors")]
[ConfigurationCollection(typeof(ActorElement), CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
public ActorsCollection ActorsCollection
{
get { return (ActorsCollection)base["actors"]; }
set { base["actors"] = value; }
}
}
//对应元素director
public class DirectorSection : ConfigurationElement
{
//对应元素director的属性firstName
[ConfigurationProperty("firstName", IsRequired = true, IsKey = true)]
public string FirstName
{
get { return base["firstName"].ToString(); }
set { base["firstName"] = value; }
}
//对应元素director的属性lastName
[ConfigurationProperty("lastName", IsRequired = true)]
public string LastName
{
get{ return base["lastName"].ToString(); }
set{ base["lastName"] = value; }
}
}
//对应元素actor
public class ActorElement : ConfigurationElement
{
//对应元素actor的属性name
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name
{
get { return base["name"].ToString(); }
set { base["name"] = value; }
}
//对应元素actor的属性roleName
[ConfigurationProperty("roleName", IsRequired = true)]
public string RoleName
{
get { return base["roleName"].ToString(); }
set { base["roleName"] = value; }
}
}
//对应标签数据集actors,需要从ConfigurationElementCollection类继承一个自定义类,
//然后要实现此类GetElementKey(ConfigurationElement element)和ConfigurationElement CreateNewElement()两个方法;
//为了方便的访问子节点可以在这个类里面定义只读的索引器。
public class ActorsCollection : ConfigurationElementCollection
{
protected override object GetElementKey(ConfigurationElement element)
{
return ((ActorElement)element).Name;
}
protected override ConfigurationElement CreateNewElement()
{
return new ActorElement();
}
public ActorElement this[int i]
{
get { return (ActorElement)base.BaseGet(i); }
}
public ActorElement this[string key]
{
get { return (ActorElement)base.BaseGet(key); }
}
}
}
在程序文件中调用如下:
ConfigFilm config = ConfigurationManager.GetSection("Film") as ConfigFilm;
Console.WriteLine("Film: " + config.FName + " " + config.Year);
Console.WriteLine("Film Director: " + config.Director.FirstName + " " + config.Director.LastName);
for (int i = 0; i < config.ActorsCollection.Count; i++)
Console.WriteLine("Film Actor: " + config.ActorsCollection[i].Name + "--" + config.ActorsCollection[i].RoleName);
以上,基本能够满足所有配置的要求。不过为了程序的可读性,我们希望能够将一些section
进行分组管理,这就需要使用sectionGroup
,我们可以自定义sectionGroup
,然后在其上配置多个section
,如我们配置包含film
和teleplay
两个section
的sectionGroup
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!--自定义数据结构-->
<sectionGroup name="Media" type="ConfigExample.ConfigGroupMedia, ConfigExample">
<section name="Film" type="ConfigExample.ConfigFilm, ConfigExample"/>
<section name="teleplay" type="ConfigExample.ConfigTeleplay, ConfigExample"/>
</sectionGroup>
</configSections>
<Media>
<Film fName="wujiandao" year ="2002">
<director firstName="Liu" lastName="weiqiang"/>
<actors>
<add name="Liang chaowei" roleName="chen yongren"/>
<add name="Liu dehua" roleName="liu jianming"/>
</actors>
</Film>
<teleplay name="yongzhengwangchao" year="1999"/>
</Media>
</configuration>
为了方便存取sectionGroup
中section
,需要实现一个继承自System.Configuration.ConfiguraionSectionGroup
的自定义类,如下:
using System.Configuration;
namespace ConfigExample
{
public class ConfigGroupMedia : ConfigurationSectionGroup
{
public ConfigFilm Film
{
get { return (ConfigFilm)base.Sections["film"]; }
}
public ConfigTeleplay Teleplay
{
get { return (ConfigTeleplay)base.Sections["teleplay"]; }
}
}
}
程序文件中调用
ConfigGroupMedia sectiongroupmedia = (ConfigGroupMedia)ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).SectionGroups["Media"];
ConfigFilm configFilm = sectiongroupmedia.Film;
ConfigTeleplay configTeleplay = sectiongroupmedia.Teleplay;