1 <system.net> 2 <mailSettings> 3 <smtp from="Fish.Q.Li@newegg.com"> 4 <network /> 5 </smtp> 6 </mailSettings> 7 </system.net>
读取上面↑的xml代码{
1 mtpSection section = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection; 2 labMailFrom.Text = "Mail From: " + section.From;
}
写入配置文件:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); SmtpSection section = config.GetSection("system.net/mailSettings/smtp") as SmtpSection; section.From = "Fish.Q.Li@newegg.com2"; config.Save();
如果配置文件为以下格式的:
<MySection444> <add key="aa" value="11111"></add> <add key="bb" value="22222"></add> <add key="cc" value="33333"></add> </MySection444>
读取方法:
1 public class MySection4 : ConfigurationSection // 所有配置节点都要选择这个基类 2 { 3 private static readonly ConfigurationProperty s_property 4 = new ConfigurationProperty(string.Empty, typeof(MyKeyValueCollection), null, 5 ConfigurationPropertyOptions.IsDefaultCollection); 6 7 [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)] 8 public MyKeyValueCollection KeyValues 9 { 10 get 11 { 12 return (MyKeyValueCollection)base[s_property]; 13 } 14 } 15 } 16 17 18 [ConfigurationCollection(typeof(MyKeyValueSetting))] 19 public class MyKeyValueCollection : ConfigurationElementCollection // 自定义一个集合 20 { 21 // 基本上,所有的方法都只要简单地调用基类的实现就可以了。 22 23 public MyKeyValueCollection() : base(StringComparer.OrdinalIgnoreCase) // 忽略大小写 24 { 25 } 26 27 // 其实关键就是这个索引器。但它也是调用基类的实现,只是做下类型转就行了。 28 new public MyKeyValueSetting this[string name] 29 { 30 get 31 { 32 return (MyKeyValueSetting)base.BaseGet(name); 33 } 34 } 35 36 // 下面二个方法中抽象类中必须要实现的。 37 protected override ConfigurationElement CreateNewElement() 38 { 39 return new MyKeyValueSetting(); 40 } 41 protected override object GetElementKey(ConfigurationElement element) 42 { 43 return ((MyKeyValueSetting)element).Key; 44 } 45 46 // 说明:如果不需要在代码中修改集合,可以不实现Add, Clear, Remove 47 public void Add(MyKeyValueSetting setting) 48 { 49 this.BaseAdd(setting); 50 } 51 public void Clear() 52 { 53 base.BaseClear(); 54 } 55 public void Remove(string name) 56 { 57 base.BaseRemove(name); 58 } 59 } 60 61 public class MyKeyValueSetting : ConfigurationElement // 集合中的每个元素 62 { 63 [ConfigurationProperty("key", IsRequired = true)] 64 public string Key 65 { 66 get { return this["key"].ToString(); } 67 set { this["key"] = value; } 68 } 69 70 [ConfigurationProperty("value", IsRequired = true)] 71 public string Value 72 { 73 get { return this["value"].ToString(); } 74 set { this["value"] = value; } 75 } 76 }
下面是demo方法
1 public class EsbHeaders : ConfigurationElement 2 { 3 [ConfigurationProperty("children", IsDefaultCollection = true)] 4 [ConfigurationCollection(typeof(EsbHeader), AddItemName = "header")] 5 public EsbHeaderCollection Headers 6 { 7 get { return (EsbHeaderCollection)base["children"]; } 8 set { base["children"] = value; } 9 } 10 } 11 12 public class EsbHeaderCollection : ConfigurationElementCollection 13 { 14 protected override ConfigurationElement CreateNewElement() 15 { 16 return new EsbHeader(); 17 } 18 19 protected override object GetElementKey(ConfigurationElement element) 20 { 21 return ((EsbHeader)element).Identity; 22 } 23 24 25 public new EsbHeader this[string key] 26 { 27 get { return (EsbHeader)base.BaseGet(key); } 28 } 29 30 public EsbHeader this[int index] 31 { 32 get { return (EsbHeader)base.BaseGet(index); } 33 } 34 35 public List<EsbHeader> ToList() 36 { 37 var list = new List<EsbHeader>(); 38 IEnumerator ie = this.GetEnumerator(); 39 while (ie.MoveNext()) 40 list.Add(ie.Current as EsbHeader); 41 return list; 42 } 43 } 44 45 public class EsbHeader : ConfigurationElement, IRequestEntity 46 { 47 private string identity = string.Empty; 48 [ConfigurationProperty("identity", IsRequired = false)] 49 public string Identity 50 { 51 get 52 { 53 if (identity == string.Empty) 54 return this.SERVICE_CODE; 55 else 56 return identity; 57 } 58 set { identity = value; } 59 } 60 61 62 [ConfigurationProperty("service", IsRequired = true)] 63 public string SERVICE_CODE 64 { 65 get { return (string)base["service"]; } 66 set { base["service"] = value; } 67 } 68 69 [ConfigurationProperty("version", IsRequired = true)] 70 public string VERSION 71 { 72 get { return (string)base["version"]; } 73 set { base["version"] = value; } 74 } 75 76 [ConfigurationProperty("consumer", IsRequired = true)] 77 public string CONSUMER_CODE 78 { 79 get { return (string)base["consumer"]; } 80 set { base["consumer"] = value; } 81 } 82 83 [ConfigurationProperty("password", IsRequired = true)] 84 public string PASSWORD 85 { 86 get { return (string)base["password"]; } 87 set { base["password"] = value; } 88 } 89 90 [ConfigurationProperty("desc", IsRequired = true)] 91 public string Description 92 { 93 get { return (string)base["desc"]; } 94 set { base["desc"] = value; } 95 } 96 97 98 [ConfigurationProperty("ext1", IsRequired = false)] 99 public string EXT1 100 { 101 get { return (string)base["ext1"]; } 102 set { base["ext1"] = value; } 103 } 104 [ConfigurationProperty("ext2", IsRequired = false)] 105 public string EXT2 106 { 107 get { return (string)base["ext2"]; } 108 set { base["ext2"] = value; } 109 } 110 [ConfigurationProperty("ext3", IsRequired = false)] 111 public string EXT3 112 { 113 get { return (string)base["ext3"]; } 114 set { base["ext3"] = value; } 115 }
demo-config
1 <sinosoft> 2 <esb> 3 <headers> 4 <children> 5 <header service="s-1" version="1" consumer="CS-1/65/" password="12436798" desc=""></header> 6 <header service="s-2" version="1" consumer="CS-1/6" password="4235465687" desc=""></header> 7 <header service="s-3" version="1" consumer="CS-1/-01" password="24346" desc=""></header> 8 9 </children> 10 </headers> 11 </esb> 12 <reports> 13 <sms url="" userid="TCBB" password="456"></sms> 14 </reports> 15 </sinosoft>
小结:
1. 为每个集合中的参数项创建一个从ConfigurationElement继承的派生类。
2. 为集合创建一个从ConfigurationElementCollection继承的集合类,具体在实现时主要就是调用基类的方法。
3. 在创建ConfigurationSection的继承类时,创建一个表示集合的属性就可以了,注意[ConfigurationProperty]的各参数。
参考自:http://www.cnblogs.com/fish-li/archive/2011/12/18/2292037.html#_labelStart