[导入]创建新的配置节
您可以用自己的 XML 配置标记扩展标准的 ASP.NET 配置设置集。若要完成该操作,您必须创建自己的配置节处理程序。该处理程序必须是一个实现 IConfigurationSectionHandler 接口的 .NET Framework 类。节处理程序解释并处理 Web.config 文件特定部分中 XML 标记中定义的设置并根据配置设置返回适当的配置对象。处理程序类返回的配置对象可以是任何数据结构;它不限于任何基配置类或配置格式。
下面的示例定义 IConfigurationSectionHandler 接口。
[Visual Basic]
Namespace System.Web.Configuration
Public Interface IConfigurationSectionHandler
Function Create(parent As Object, input As Object, _
node As XmlNode) As Object
End Interface
End Namespace
[C#]
namespace System.Web.Configuration
{
public interface IConfigurationSectionHandler
{
public Object Create(Object parent, Object input,
XmlNode node);
}
}
您还可以定义自己的节,该节与 <appSettings> 节使用相同的配置处理程序。例如:
<configuration>
<configSections>
<sectionGroup name="myGroup">
<sectionGroup name="nestedGroup">
<section name="mySection" type=
"System.Configuration.NameValueSectionHandler,System"/>
</sectionGroup>
</sectionGroup>
</configSections>
<myGroup>
<nestedGroup>
<mySection>
<add key="key_one" value="1"/>
<add key="key_two" value="2"/>
</mySection>
</nestedGroup>
</myGroup>
</configuration>
您可以读取上面的示例中定义的新配置节的值,如下:
[Visual Basic]
Dim config As NameValueCollection =
ConfigurationSettings.GetConfig("myGroup/nestedGroup/mySection")
Response.Write("The value of key_one is " & config("key_one") & "<br>")
Response.Write("The value of key_two is " & config("key_two") & "<br>")
[C#]
NameValueCollection config = (NameValueCollection)
ConfigurationSettings.GetConfig("myGroup/nestedGroup/mySection");
Response.Write("The value of key_one is " + config["key_one"] + "<br>");
Response.Write("The value of key_two is " + config["key_two"] + "<br>");
文章来源:http://www.codeguru.cn/ShowThread.aspx?PostID=1288
浙公网安备 33010602011771号