配置文件configSections节点使用实例 。
2016-11-15 17:34
411 查看
configSections为自定义节点,增加应用程序可移植性,用于配置文件上传路径,再深入应用可定义工厂方法需要加载创建的类。
1.配置configSections节点
[html]
view plain
copy
print?
<configSections>
<section name ="MyName" type="LearningConfiguration.NameSectionHandler"/>
</configSections>
<MyName>
<Add key="lu" name="lulu"></Add>
<Add key="lu2" name="66"></Add>
</MyName>
2.定义NameSectionHandler类实现IConfigurationSectionHandler接口
[csharp]
view plain
copy
print?
namespace LearningConfiguration
{
public class NameSectionHandler : IConfigurationSectionHandler
{
#region 隐式实现接口
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
Dictionary<string, string> names = new Dictionary<string, string>();
string key = string.Empty;
string name = string.Empty;
//获取配置文件中自定义节点值
foreach (XmlNode childNode in section.ChildNodes)
{
if (childNode.Attributes["key"] != null)
{
key = childNode.Attributes["key"].Value;
if (childNode.Attributes["name"] != null)
{
name = childNode.Attributes["name"].Value;
}
else
{
name = string.Empty;
}
names.Add(key, name);
}
}
return names;
}
#endregion
}
}
3.调用
[csharp]
view plain
copy
print?
protected void Page_Load(object sender, EventArgs e)
{
Dictionary<string, string> names = ConfigurationManager.GetSection("MyName") as Dictionary<string, string>;
if (names != null)
{
//输出:lulu66
foreach (string key in names.Keys)
{
Response.Write(names[key]);
}
}
}
1.配置configSections节点
[html]
view plain
copy
print?
<configSections>
<section name ="MyName" type="LearningConfiguration.NameSectionHandler"/>
</configSections>
<MyName>
<Add key="lu" name="lulu"></Add>
<Add key="lu2" name="66"></Add>
</MyName>
<configSections> <section name ="MyName" type="LearningConfiguration.NameSectionHandler"/> </configSections> <MyName> <Add key="lu" name="lulu"></Add> <Add key="lu2" name="66"></Add> </MyName>
2.定义NameSectionHandler类实现IConfigurationSectionHandler接口
[csharp]
view plain
copy
print?
namespace LearningConfiguration
{
public class NameSectionHandler : IConfigurationSectionHandler
{
#region 隐式实现接口
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
Dictionary<string, string> names = new Dictionary<string, string>();
string key = string.Empty;
string name = string.Empty;
//获取配置文件中自定义节点值
foreach (XmlNode childNode in section.ChildNodes)
{
if (childNode.Attributes["key"] != null)
{
key = childNode.Attributes["key"].Value;
if (childNode.Attributes["name"] != null)
{
name = childNode.Attributes["name"].Value;
}
else
{
name = string.Empty;
}
names.Add(key, name);
}
}
return names;
}
#endregion
}
}
namespace LearningConfiguration { public class NameSectionHandler : IConfigurationSectionHandler { #region 隐式实现接口 public object Create(object parent, object configContext, System.Xml.XmlNode section) { Dictionary<string, string> names = new Dictionary<string, string>(); string key = string.Empty; string name = string.Empty; //获取配置文件中自定义节点值 foreach (XmlNode childNode in section.ChildNodes) { if (childNode.Attributes["key"] != null) { key = childNode.Attributes["key"].Value; if (childNode.Attributes["name"] != null) { name = childNode.Attributes["name"].Value; } else { name = string.Empty; } names.Add(key, name); } } return names; } #endregion } }
3.调用
[csharp]
view plain
copy
print?
protected void Page_Load(object sender, EventArgs e)
{
Dictionary<string, string> names = ConfigurationManager.GetSection("MyName") as Dictionary<string, string>;
if (names != null)
{
//输出:lulu66
foreach (string key in names.Keys)
{
Response.Write(names[key]);
}
}
}
protected void Page_Load(object sender, EventArgs e) { Dictionary<string, string> names = ConfigurationManager.GetSection("MyName") as Dictionary<string, string>; if (names != null) { //输出:lulu66 foreach (string key in names.Keys) { Response.Write(names[key]); } } }
相关文章推荐
- 配置文件configSections节点使用实例
- Spring.net在.NET应用程序的标准配置文件中用一个自定义节点来配置容器实例
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- 使用C#实现读取系统配置文件的代码实例讲解
- php经典实例使用正则动态修改配置文件
- 自动生成NHibernate配置文件工具的使用实例
- spring List,Set,Map,Properties,array的使用配置文件注入实例
- 使用Properties文件配置实例的创建
- ASP.NET配置文件里经常使用到的节点信息
- Spring AMQP实例 以及Spring 配置文件动态注入属性使用实战
- Asp.net使用代码修改配置文件的节点值
- Sping2.5配置文件的单例共享模式与独立产生实例,同步方法使用
- Python中使用ConfigParser解析ini配置文件实例
- 使用OpenCV官方提供文件配置、静态编译并运行OpenCV-2.3.x/2.4.x实例
- Redis(Windows安装方法与Java调用实例 & 配置文件参数说明 & Java使用Redis所用Jar包 & Redis与Memcached区别 & redis-cli.exe命令及示例)
- Spring的Ioc简单使用实例(含XML配置文件路径知识)
- 使用OpenCV官方提供文件配置、静态编译并运行OpenCV-2.3.x/2.4.x实例
- 使用OpenCV官方提供文件配置、静态编译并运行OpenCV-2.3.x/2.4.x实例
- C# WinForm开发中使用XML配置文件实例
- spring 框架中的依赖注入(IOC--设值注入)---使用xml简单配置文件---的具体实例的简单实现