config文件 - Property
先来看最简单的自定义节点,每个配置值以属性方式存在:
<MySection111 username="fish-li" url="http://www.cnblogs.com/fish-li/"></MySection111>
实现代码如下:
public class MySection1 : ConfigurationSection
{
[ConfigurationProperty("username", IsRequired = true)]
public string UserName
{
get { return this["username"].ToString(); }
set { this["username"] = value; }
}
[ConfigurationProperty("url", IsRequired = true)]
public string Url
{
get { return this["url"].ToString(); }
set { this["url"] = value; }
}
}
{
[ConfigurationProperty("username", IsRequired = true)]
public string UserName
{
get { return this["username"].ToString(); }
set { this["username"] = value; }
}
[ConfigurationProperty("url", IsRequired = true)]
public string Url
{
get { return this["url"].ToString(); }
set { this["url"] = value; }
}
}
小结:
①自定义一个类,以ConfigurationSection为基类,各个属性要加上[ConfigurationProperty] ,ConfigurationProperty的构造函数中传入的name字符串将会用于config文件中,表示各参数的属性名称。
②属性的值的读写要调用this[],由基类去保存,请不要自行设计Field来保存。
③为了能使用配置节点能被解析,需要在中注册: ,且要注意name="MySection111"要与是对应的。
说明:下面将要介绍另三种配置节点,虽然复杂一点,但是一些基础的东西与这个节点是一样的,所以后面我就不再重复说明了。
config文件 - Element
再来看个复杂点的,每个配置项以XML元素的方式存在:
<MySection222>
<users username="fish" password="liqifeng"></users>
</MySection222>
<users username="fish" password="liqifeng"></users>
</MySection222>
实现代码如下:
public class MySection2 : ConfigurationSection
{
[ConfigurationProperty("users", IsRequired = true)]
public MySectionElement Users
{
get { return (MySectionElement)this["users"]; }
}
}
public class MySectionElement : ConfigurationElement
{
[ConfigurationProperty("username", IsRequired = true)]
public string UserName
{
get { return this["username"].ToString(); }
set { this["username"] = value; }
}
[ConfigurationProperty("password", IsRequired = true)]
public string Password
{
get { return this["password"].ToString(); }
set { this["password"] = value; }
}
}
{
[ConfigurationProperty("users", IsRequired = true)]
public MySectionElement Users
{
get { return (MySectionElement)this["users"]; }
}
}
public class MySectionElement : ConfigurationElement
{
[ConfigurationProperty("username", IsRequired = true)]
public string UserName
{
get { return this["username"].ToString(); }
set { this["username"] = value; }
}
[ConfigurationProperty("password", IsRequired = true)]
public string Password
{
get { return this["password"].ToString(); }
set { this["password"] = value; }
}
}
小结:
自定义一个类,以ConfigurationSection为基类,各个属性除了要加上[ConfigurationProperty]
类型也是自定义的,具体的配置属性写在ConfigurationElement的继承类中。
config文件 - CDATA
有时配置参数包含较长的文本,比如:一段SQL脚本,或者一段HTML代码,那么,就需要CDATA节点了。假设要实现一个配置,包含二段SQL脚本:
<MySection333>
<Command1>
<![CDATA[
create procedure ChangeProductQuantity(
@ProductID int,
@Quantity int
)
as
update Products set Quantity = @Quantity
where ProductID = @ProductID;
]]>
</Command1>
<Command2>
<![CDATA[
create procedure DeleteCategory(
@CategoryID int
)
as
delete from Categories
where CategoryID = @CategoryID;
]]>
</Command2>
</MySection333>
<Command1>
<![CDATA[
create procedure ChangeProductQuantity(
@ProductID int,
@Quantity int
)
as
update Products set Quantity = @Quantity
where ProductID = @ProductID;
]]>
</Command1>
<Command2>
<![CDATA[
create procedure DeleteCategory(
@CategoryID int
)
as
delete from Categories
where CategoryID = @CategoryID;
]]>
</Command2>
</MySection333>