如何使用.NET配置文件(二)

    .NET的应用程序配置文件,使用的是XML格式。相对INI文件来说,它的功能要强上不少,而且具有很强的可扩展性。它的缺点是不能直接进行写操作,也就是说,不能直接在程序中修改配置文件的数据(当然不是指不能,不过不是本文讨论的范围)。本文主要目的是探讨如何扩展配置文件,并在其加入各种自定义配置信息。

如何使用.NET配置文件(一)    如何使用.NET配置文件(二)    沐枫网志


    3. 自定义配置结构 (使用IConfigurationSectionHandler)
 假设有以下的配置信息,其在MyInfo可以重复许多次,那么应如何读取配置呢?这时就要使用自定义的配置程序了。

<myConfigs>
  
<myInfo Area="Fuzhou" Device="Printer" Customer="Muf" />
  
<myInfo Area="Shanghai" Device="Mobile" Customer="Liny" />
</myConfig>

 访问代码如下:

Hashtable cfgTable = (Hashtable)ConfigurationSettings.GetConfig( "myConfigs" );

Debug.Assert( cfgTable.Count 
== 2);
Hashtable cfgFuzhou 
= (Hashtable)cfgTable["Fuzhou"];
Hashtable cfgShanghai 
= (Hashtable)cfgTable["Shanghai"];
Debug.Assert( cfgFuzhou[
"Device"== "Printer" );
Debug.Assert( cfgShanghai[
"Device"== "Mobile" );
Debug.Assert( cfgFuzhou[
"Customer"== "Muf" );
Debug.Assert( cfgShanghai[
"Customer"== "Liny" );

foreach(Hashtable cfg in cfgTable.Values)
{
 Console.WriteLine(
"Area={0} Device={1} Customer={2}", cfg["Area"], cfg["Device"], cfg["Customer"]);
}


 为了能使用上面的访问代码来访问配置结构,我们需要生成一个特定的配置读取类(ConfigurationSectionHandler),例子很简单,就不多做说明了:

public class MyInfoSectionHandler: IConfigurationSectionHandler
{
 
public object Create(object parent, object configContext, System.Xml.XmlNode section)
 
{
  Hashtable config 
= new Hashtable();
  
foreach(XmlNode node in section.ChildNodes)
  
{
   
if(node.Name != "myInfo")
    
throw new System.Configuration.ConfigurationException("不可识别的配置项", node);

   Hashtable item 
= new Hashtable();
   
foreach(XmlAttribute attr in node.Attributes)
   
{
    
switch(attr.Name)
    
{
     
case "Area":
     
case "Device":
     
case "Customer":
      item.Add(attr.Name, attr.Value);
      
break;
     
default:
      
throw new System.Configuration.ConfigurationException("不可识别的配置属性", attr);
    }

   }

   config.Add(item[
"Area"], item);
  }

  
return config;
 }

}


 然后,我们再定义配置说明。其中,myNamespace.MyInfoSectionHandler 是MyInfoSectionHandler类的带名字空间的完整名称;myApp 则是定义MyInfoSectionHandler类的程序集不带扩展名的名字(如myApp.dll或myApp.exe):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  
<!-- 以下是自定义配置的声明 -->
  
<configSections>
      
<section name="myConfig" type="myNamespace.MyInfoSectionHandler, myApp" />
  
</configSections>   
  
<myConfigs>
    
<myInfo Area="Fuzhou" Device="Printer" Customer="Muf" />
    
<myInfo Area="Shanghai" Device="Mobile" Customer="Liny" />
  
</myConfig>
</configuration>

 根据上面的例子,我们可以使用IConfigurationSectionHandler来实现任意的配置文件结构。

posted @ 2005-09-06 16:39 沐枫 阅读(2585) 评论(10)  编辑 收藏 网摘 所属分类: .NET

  回复  引用    
#1楼 2005-09-07 08:16 | erpcrm@163.com [未注册用户]
朋友,你对.net2.0没研究吗?
.net2.0新增加了一组API,不但能够写回,而且,框架也变了不用实现IConfigurationSectionHandler接口了,而是提供了一组丰富的基类

  回复  引用  查看    
#2楼 2005-09-07 08:54 | 仁面寿星      
谢谢了,这些东西对我们这种新手很有帮助。
  回复  引用    
#3楼 2005-09-07 09:21 | 沐枫 [未注册用户]
问题是.NET20还是Beta版啊。
对于目前大量使用的.NET1.0和.NET1.1,远水解不了进渴啊。

  回复  引用  查看    
#4楼 2005-09-07 09:31 | oop80      
很详细,辛苦了。
  回复  引用  查看    
#5楼 2005-09-07 11:31 | bugSharp      
是很详细了。
配置文件中加入:
<configSections>
<section name="myConfig" type="myNamespace.MyInfoSectionHandler, myApp" />
</configSections>

在代码中使用
Hashtable cfgTable = (Hashtable)ConfigurationSettings.GetConfig( "myConfigs" );
.net CLR就会自动调用 配置读取类ConfigurationSectionHandler中的 Create方法,把myCofig配置节的内容读到Hashtable中。

不清楚的是,Create方法的自动调用,.net是怎么实现的呢?

  回复  引用    
#6楼 2005-09-07 13:20 | 沐枫 [未注册用户]
@bugSharp
.NET配置程序会根据type的值,创建相应的类型,并把对应的参数传递给Create。
具体的实现,你如果感兴趣,可以对.NET的库进行反编译。:)

  回复  引用  查看    
#7楼 2005-09-07 15:44 | 把我的欢乐带给你      

good


  回复  引用    
#8楼 2005-12-05 13:42 | Carl [未注册用户]
不错,学到了东西。
  回复  引用  查看    
#9楼 2006-07-06 18:35 | Anderslly      
多谢了,你的例子让我理解了Create()方法的作用
  回复  引用    
#10楼 2007-11-05 21:17 | 光源三 [未注册用户]
谢谢了

发表评论



姓名 [登录] [注册] 
主页
Email (仅博主可见) 
验证码 *  验证码看不清,换一张
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论   新用户注册   返回页首      

导航: 网站首页 社区 新闻 博问 闪存 网摘 招聘 .NET频道 知识库 找找看 Google站内搜索



China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
China-Pub 计算机绝版图书按需印刷服务

相关文章:

相关链接: