maomaocat

导航

.Net2.0配置文档修改示例

         在工作中遇到.net2.0配置文档自定义节读写问题。现作一简单示例。
        1.首先熟悉System.Configuration命名空间的以下几个类:ConfigurationSection,ConfigurationElement,ConfigurationElementCollection,ConfigurationProperty,ConfigurationPropertyCollection,它们是经常用到的,还有一些类(eg:ConfigurationSectionGroup等),可以看MSDN
(ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.NETDEVFX.v20.chs/cpref4/html/T_System_Configuration_ConfigurationSection.htm.)
       2.有几点要注意:
                   节:节最简单的形式与基本元素一致。而复杂的节可以包括一个或多个基本元素、元素的集合以及其他节可以扩展 ConfigurationElement 类表示 ConfigurationSection 节中的基本元素。扩展ConfigurationElementCollection表示相应的元素集合。
                  属性:在较为复杂的配置元素,ConfigurationProperty 对象既可以表示 ConfigurationElement 对象,也可以表示属性。

    以下是配置文档:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="SectionExample" type="SimpleConfig.PanSection, SimpleConfig" />
  </configSections>
  <SectionExample>
    <SectionElementCollection>
      <add BranchID="1" BranchNo="1" SubCenterNo="0" WebServiceUrl="localhost\test1" />
      <add BranchID="2" BranchNo="2" SubCenterNo="0" WebServiceUrl="localhost\test2tttokdd" />
      <add BranchID="3" BranchNo="3" SubCenterNo="0" WebServiceUrl="localhost\test3testyy" />
    </SectionElementCollection>
  </SectionExample>
</configuration>

我们可以用如下类表示:

 

 public sealed class PanSectionElement : System.Configuration.ConfigurationElement
    {
        
        [ConfigurationProperty(
"BranchID")]
        
public int BranchID
        {
            
get { return (int)this["BranchID"]; }
            
set { this["BranchID"= value; }
        }
        [ConfigurationProperty(
"BranchNo")]
        
public int BranchNo
        {
            
get { return (int)this["BranchNo"]; }
            
set { this["BranchNo"= value; }
        }
        [ConfigurationProperty(
"SubCenterNo")]
        
public int SubCenterNo
        {
            
get { return (int)this["SubCenterNo"]; }
            
set { this["SubCenterNo"= value; }
        }
        [ConfigurationProperty(
"WebServiceUrl")]
        
public string WebServiceUrl
        {
            
get { return (string)this["WebServiceUrl"]; }
            
set { this["WebServiceUrl"= value; }

        }
       
    }

[ConfigurationCollection(
typeof(PanSectionElement))]
    
public sealed class PanSectionElementCollection : ConfigurationElementCollection
    {
       
        
static PanSectionElementCollection()
        {
                 
        }
      

        
#region 公共属性
        
        
/// <summary>
        
/// 按照关键值提取配置信息
        
/// </summary>
        
/// <param name="KeyName"></param>
        
/// <returns></returns>
        public new PanSectionElement this[string Key]
        {
            
get
            {
                
return (PanSectionElement)base.BaseGet(Key);
            }
            
set
            {
                
base.BaseRemove(Key);
                
this.BaseAdd(value);
            }
        }
        
#endregion
        
protected override ConfigurationElement CreateNewElement()
        {
            
return new PanSectionElement();
        }

        
protected override object GetElementKey(ConfigurationElement element)
        {
            
string key = ((PanSectionElement)element).BranchID.ToString();



            
return key;
        }
    }


public sealed class PanSection : ConfigurationSection
    {
        
#region 私有的静态变量
        
private static readonly ConfigurationProperty _propItems;
        
private static ConfigurationPropertyCollection _properties;
        
#endregion
         
#region 构造函数
        
static PanSection()
        {
            
// 定义属性

            _propItems 
= new ConfigurationProperty("SectionElementCollection"typeof(PanSectionElementCollection), null, ConfigurationPropertyOptions.IsRequired);
            
// 把用户属性加入到属性集合中
            _properties = new ConfigurationPropertyCollection();
            _properties.Add(_propItems);
        }
        
#endregion
        
#region 公共属性
       
        
/// <summary>
        
/// 这个属性表示配置文件中的一个配置节中的集合属性 <code>Items</code>
       
/// </summary>
        [ConfigurationProperty("SectionElementCollection")]
        
public PanSectionElementCollection SectionElementCollection
        {
            
get { return (PanSectionElementCollection)base[_propItems]; }
            
set { base[_propItems] = value; }
        }
         
#endregion

        
// This is a key customization. 
        
// It returns the initialized property bag.
        protected override ConfigurationPropertyCollection Properties
        {
            
get
            {
                
return _properties;
            }
        }

    }



读取与写入相应就简单了:
相应的代码:

读取:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            SimpleConfig.PanSection section 
= config.GetSection("SectionExample") as SimpleConfig.PanSection;

            
string elementKey = pdv;
            
//获取相应的集合的配置元素的值
            SimpleConfig.PanSectionElement propItem = section.SectionElementCollection[elementKey];
            
//以下略 

写入:
SimpleConfig.PanSectionElement propItem = section.SectionElementCollection[elementKey];
            try
            
{   //属性赋值.
                propItem.BranchNo 
= System.Int32.Parse(txtBranchNo.Text.Trim());
                propItem.SubCenterNo 
= System.Int32.Parse(txtSubcenterNo.Text.Trim());
                propItem.WebServiceUrl 
= txtWebServiceUrl.Text;

            }

            
catch (Exception)
            
{
                MessageBox.Show(
"请输入正确格式的元素属性""警告");
                
return;

            }

            config.Save();
            //
保存相应的信息


ConfigurationSectionGroup表示配置文件中相关节的一种分组方式。个人认为使用价值不大,略过。

(如有朋友需要我会上传示例的源码)

posted on 2007-05-12 09:07  maomaocat  阅读(511)  评论(1)    收藏  举报