天天看点

.Net FrameWork2.0 以后的配置技术(System.Configuration)

应为编码需要,要在配置文件中增加如下的内容

 需求

 <configuration>

<configSections>

<sectionGroup name="GenerateHtmlConfig">

<section name="GenerateHtmlConfigSection" type="GenerateHtmlLib.Config.SectionHandler,GenerateHtmlLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>

</sectionGroup>

</configSections>

<GenerateHtmlConfig>

<GenerateHtmlConfigSection isAvailable="true" TimeSpan="3">

<myChildSection myChildAttrib1="Zippy" myChildAttrib2="Michael Zawondy"/>

<myChildConfigElementList>

<clear />

<add myChildAttrib1="Zippy" myChildAttrib2="1"/>

<add myChildAttrib1="222" myChildAttrib2="2"/>

</myChildConfigElementList>

</GenerateHtmlConfigSection>

</GenerateHtmlConfig>

...

</configuration>

这里面用到了常用的节点,节点属性,子元素,子元素集合

自定义节点的编码需要继承ConfigurationSection类

自定义子元素需要继承configurationelement

自定义子元素集合需要继承configurationelementcollection

节点属性的例子在ConfigurationSection类的msdn中有

代码

配置信息自定义

using System;

using System.Collections;

using System.Configuration;

using System.Xml;

namespace GenerateHtmlLib.Config

{

public class SectionHandler : ConfigurationSection

{

/// <summary>

/// 本配置是否可用

/// </summary>

[ConfigurationProperty("isAvailable", DefaultValue = true, IsRequired = false)]

public Boolean IsAvailable

{

get { return (Boolean)this["isAvailable"]; }

set { this["isAvailable"] = value; }

}

/// <summary>

/// 间隔时间

/// </summary>

[ConfigurationProperty("TimeSpan", DefaultValue = 5, IsRequired = false)]

[IntegerValidator(MinValue = 1)]//排除负数

public int TimeSpan

{

get { return (int)this["TimeSpan"]; }

}

/// <summary>

/// 子元素,默认转换规则

/// </summary>

[ConfigurationProperty("myChildSection")]

public MyChildConfigElement MyChildSection

{

get

{ return (MyChildConfigElement)this["myChildSection"]; }

set

{ this["myChildSection"] = value; }

}

/// <summary>

/// 子元素集合,转换规则的集合

/// </summary>

[ConfigurationProperty("myChildConfigElementList")]

public MyChildConfigElementList MyChildSectionList

{

get

{

return this["myChildConfigElementList"] as MyChildConfigElementList;

}

set

{

this["myChildConfigElementList"] = value;

}

}

public class MyChildConfigElement : ConfigurationElement

{

public MyChildConfigElement()

{

}

public MyChildConfigElement(String a1, String a2)

{

MyChildAttribute1 = a1;

MyChildAttribute2 = a2;

}

[ConfigurationProperty("myChildAttrib1", DefaultValue = "Zippy", IsRequired = true)]

[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'/"|//", MinLength = 1, MaxLength = 60)]

public String MyChildAttribute1

{

get

{ return (String)this["myChildAttrib1"]; }

set

{ this["myChildAttrib1"] = value; }

}

[ConfigurationProperty("myChildAttrib2", DefaultValue = "Michael Zawondy", IsRequired = true)]

[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'/"|//", MinLength = 1, MaxLength = 60)]

public String MyChildAttribute2

{

get

{ return (String)this["myChildAttrib2"]; }

set

{ this["myChildAttrib2"] = value; }

}

}

public sealed class MyChildConfigElementList : ConfigurationElementCollection

{

public MyChildConfigElement this[int index]

{

get

{

return base.BaseGet(index) as MyChildConfigElement;

}

set

{

if (index < base.Count)

{

if (base.BaseGet(index) != null)

{

base.BaseRemoveAt(index);

}

this.BaseAdd(index, value);

}

}

}

protected override ConfigurationElement CreateNewElement()

{

return new MyChildConfigElement();

}

protected override object GetElementKey(ConfigurationElement element)

{

return ((MyChildConfigElement)element).MyChildAttribute1;

}

}

}

}

配置信息读取

 <%@ Page Language="C#" %>

<mce:script runat="server"><!--

protected void Button1_Click(object sender, EventArgs e)

{

GenerateHtmlLib.Config.SectionHandler config =

(GenerateHtmlLib.Config.SectionHandler)System.Configuration.ConfigurationManager.GetSection(

"GenerateHtmlConfig/GenerateHtmlConfigSection");

StringBuilder sb = new StringBuilder();

sb.Append("<h2>Attributes in the myCustomSection Element:</h2>");

sb.AppendFormat("IsAvailable = {0}<br/>", config.IsAvailable.ToString());

sb.AppendFormat("TimeSpan = {0}<br/>", config.TimeSpan.ToString());

sb.Append("<h2>Attributes in the myChildSection Element:</h2>");

sb.AppendFormat("myChildAttrib1 = {0}<br/>", config.MyChildSection.MyChildAttribute1.ToString());

sb.AppendFormat("myChildAttrib2 = {0}<br/>", config.MyChildSection.MyChildAttribute2.ToString());

sb.Append("<h2>Attributes in the myChildSectionList Element:</h2>");

sb.AppendFormat(".MyChildSectionList[0].MyChildAttribute1 = {0}<br/>", config.MyChildSectionList[0].MyChildAttribute1.ToString());

sb.AppendFormat(".MyChildSectionList[1].MyChildAttribute2 = {0}<br/>", config.MyChildSectionList[1].MyChildAttribute1.ToString());

sb.AppendFormat(".MyChildSectionList.Count = {0}<br/>", config.MyChildSectionList.Count.ToString());

sb.AppendFormat(".MyChildSectionList.CollectionType = {0}<br/>", config.MyChildSectionList.CollectionType.ToString());

Label1.Text = sb.ToString();

Label1.Visible = true;

}

// --></mce:script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<h1>Enumerate MyCustomSection</h1>

<asp:Label ID="Label1" runat="server"

Text="" />

<br />

<asp:Button ID="Button1" runat="server"

Text="Get Custom Config Info"

OnClick="Button1_Click" />

</div>

</form>

</body>

</html>

参考

http://hi.baidu.com/liupras/blog/item/e2ccbf50a1aef7658435240c.html

msndn的代码示例要按下面的顺序阅读和复制执行:

http://msdn.microsoft.com/zh-cn/library/2tw134k3(v=VS.90).aspx

http://msdn.microsoft.com/zh-cn/library/system.configuration.configurationelement(v=VS.90).aspx

http://msdn.microsoft.com/zh-cn/library/system.configuration.configurationelementcollection(v=VS.90).aspx

继续阅读