保存集合属性到App.config

怎样在App.config文件中,自定义节点保存如下格式的内容:
 1 None.gif <? xml version="1.0" encoding="utf-8"  ?>
 2 None.gif     < configuration >
 3 None.gif         < configSections >
 4 None.gif             < section  name ="TestSection"  type ="Test.TestSection,TestService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"   />
 5 None.gif   </ configSections >
 6 None.gif         < TestSection  name ="test"  password ="******" >
 7 None.gif    < channels >
 8 None.gif     < add  id ="one"  name ="channel_1"   />
 9 None.gif     < add  id ="two"  name ="channel_2"   />
10 None.gif     < add  id ="three"  name ="channel_3"   />
11 None.gif    </ channels >
12 None.gif   </ TestSection >
13 None.gif </ configuration >

1、元素要继承ConfigurationElement
 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.Text;
 4 None.gif using  System.Configuration;
 5 None.gif using  System.Xml;
 6 None.gif namespace  Test
 7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 9InBlock.gif    /// 通道
10ExpandedSubBlockEnd.gif    /// </summary>

11InBlock.gif    public class Channel : ConfigurationElement
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif
14InBlock.gif        public Channel() 
15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
16InBlock.gif
17ExpandedSubBlockEnd.gif        }

18InBlock.gif        public Channel(string name) : this(Guid.NewGuid().ToString(),name)
19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
20ExpandedSubBlockEnd.gif        }

21InBlock.gif        public Channel(string id ,string name)
22ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
23InBlock.gif            ID = id;
24InBlock.gif            Name = name;
25ExpandedSubBlockEnd.gif        }

26InBlock.gif
27ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
28InBlock.gif        /// 通道编号
29ExpandedSubBlockEnd.gif        /// </summary>

30InBlock.gif        [ConfigurationProperty("id",IsRequired=true)]
31InBlock.gif        public string ID
32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
33ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn (stringthis["id"]; }
34ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifthis["id"= value; }
35ExpandedSubBlockEnd.gif        }

36InBlock.gif
37ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
38InBlock.gif        /// 通道名称
39ExpandedSubBlockEnd.gif        /// </summary>

40InBlock.gif        [ConfigurationProperty("name",IsRequired=true)]
41InBlock.gif        public string Name
42ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
43ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn (stringthis["name"]; }
44ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifthis["name"= value; }
45ExpandedSubBlockEnd.gif        }

46InBlock.gif
47InBlock.gif        protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
48ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
49InBlock.gif            base.DeserializeElement(reader, serializeCollectionKey);
50ExpandedSubBlockEnd.gif        }

51InBlock.gif        protected override bool SerializeElement(XmlWriter writer, bool serializeCollectionKey)
52ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
53InBlock.gif            return  base.SerializeElement(writer, serializeCollectionKey);
54ExpandedSubBlockEnd.gif        }

55InBlock.gif
56InBlock.gif        protected override bool IsModified()
57ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
58InBlock.gif           return   base.IsModified();
59ExpandedSubBlockEnd.gif        }

60ExpandedSubBlockEnd.gif    }

61ExpandedBlockEnd.gif}

62 None.gif
2、集合要继承ConfigurationElementCollection
  1 None.gif using  System;
  2 None.gif using  System.Collections.Generic;
  3 None.gif using  System.Text;
  4 None.gif using  System.Configuration;
  5 None.gif namespace  Test
  6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  7InBlock.gif         public class ChannelCollection : ConfigurationElementCollection
  8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
  9InBlock.gif            public ChannelCollection()
 10ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 11InBlock.gif                Channel channel = (Channel)CreateNewElement();
 12InBlock.gif                Add(channel);
 13ExpandedSubBlockEnd.gif            }

 14InBlock.gif
 15InBlock.gif            public override ConfigurationElementCollectionType CollectionType
 16ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 17InBlock.gif                get
 18ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 19InBlock.gif                    return ConfigurationElementCollectionType.AddRemoveClearMap;
 20ExpandedSubBlockEnd.gif                }

 21ExpandedSubBlockEnd.gif            }

 22InBlock.gif
 23InBlock.gif            protected override ConfigurationElement CreateNewElement()
 24ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 25InBlock.gif                return new Channel();
 26ExpandedSubBlockEnd.gif            }

 27InBlock.gif
 28InBlock.gif
 29InBlock.gif            protected override ConfigurationElement CreateNewElement(string elementName)
 30ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 31InBlock.gif                return new Channel(elementName);
 32ExpandedSubBlockEnd.gif            }

 33InBlock.gif
 34InBlock.gif
 35InBlock.gif            protected override Object GetElementKey(ConfigurationElement element)
 36ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 37InBlock.gif                return ((Channel)element).Name;
 38ExpandedSubBlockEnd.gif            }

 39InBlock.gif
 40InBlock.gif
 41InBlock.gif            public new string AddElementName
 42ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 43InBlock.gif                get
 44ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gifreturn base.AddElementName; }
 45InBlock.gif
 46InBlock.gif                set
 47ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gifbase.AddElementName = value; }
 48InBlock.gif
 49ExpandedSubBlockEnd.gif            }

 50InBlock.gif
 51InBlock.gif            public new string ClearElementName
 52ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 53InBlock.gif                get
 54ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gifreturn base.ClearElementName; }
 55InBlock.gif
 56InBlock.gif                set
 57ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gifbase.AddElementName = value; }
 58InBlock.gif
 59ExpandedSubBlockEnd.gif            }

 60InBlock.gif
 61InBlock.gif            public new string RemoveElementName
 62ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 63InBlock.gif                get
 64ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gifreturn base.RemoveElementName; }
 65InBlock.gif
 66InBlock.gif
 67ExpandedSubBlockEnd.gif            }

 68InBlock.gif
 69InBlock.gif            public new int Count
 70ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 71InBlock.gif
 72ExpandedSubBlockStart.gifContractedSubBlock.gif                get dot.gifreturn base.Count; }
 73InBlock.gif
 74ExpandedSubBlockEnd.gif            }

 75InBlock.gif
 76InBlock.gif
 77InBlock.gif            public Channel this[int index]
 78ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 79InBlock.gif                get
 80ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 81InBlock.gif                    return (Channel)BaseGet(index);
 82ExpandedSubBlockEnd.gif                }

 83InBlock.gif                set
 84ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 85InBlock.gif                    if (BaseGet(index) != null)
 86ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 87InBlock.gif                        BaseRemoveAt(index);
 88ExpandedSubBlockEnd.gif                    }

 89InBlock.gif                    BaseAdd(index, value);
 90ExpandedSubBlockEnd.gif                }

 91ExpandedSubBlockEnd.gif            }

 92InBlock.gif
 93InBlock.gif            new public Channel this[string Name]
 94ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 95InBlock.gif                get
 96ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 97InBlock.gif                    return (Channel)BaseGet(Name);
 98ExpandedSubBlockEnd.gif                }

 99ExpandedSubBlockEnd.gif            }

100InBlock.gif
101InBlock.gif            public int IndexOf(Channel channel)
102ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
103InBlock.gif                return BaseIndexOf(channel);
104ExpandedSubBlockEnd.gif            }

105InBlock.gif
106InBlock.gif            public void Add(Channel channel)
107ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
108InBlock.gif                BaseAdd(channel);
109InBlock.gif
110InBlock.gif                // Add custom code here.
111ExpandedSubBlockEnd.gif            }

112InBlock.gif
113InBlock.gif            protected override void
114InBlock.gif                BaseAdd(ConfigurationElement element)
115ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
116InBlock.gif                BaseAdd(element, false);
117InBlock.gif                // Add custom code here.
118ExpandedSubBlockEnd.gif            }

119InBlock.gif
120InBlock.gif            public void Remove(Channel channel)
121ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
122InBlock.gif                if (BaseIndexOf(channel) >= 0)
123InBlock.gif                    BaseRemove(channel.Name);
124ExpandedSubBlockEnd.gif            }

125InBlock.gif
126InBlock.gif            public void RemoveAt(int index)
127ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
128InBlock.gif                BaseRemoveAt(index);
129ExpandedSubBlockEnd.gif            }

130InBlock.gif
131InBlock.gif            public void Remove(string name)
132ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
133InBlock.gif                BaseRemove(name);
134ExpandedSubBlockEnd.gif            }

135InBlock.gif
136InBlock.gif            public void Clear()
137ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
138InBlock.gif                BaseClear();
139InBlock.gif                // Add custom code here.
140ExpandedSubBlockEnd.gif            }

141ExpandedSubBlockEnd.gif        }

142ExpandedBlockEnd.gif}
3、节点要继承ConfigurationSection
 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.Text;
 4 None.gif using  System.Configuration;
 5 None.gif using  System.Xml;
 6 None.gif
 7 None.gif namespace  Test
 8 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
10InBlock.gif    /// Test配置信息
11ExpandedSubBlockEnd.gif    /// </summary>

12InBlock.gif    public class TestSection : ConfigurationSection 
13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
14ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
15InBlock.gif        /// Test名称
16ExpandedSubBlockEnd.gif        /// </summary>

17InBlock.gif        [ConfigurationProperty("name",IsRequired=true)]
18InBlock.gif        public string Name
19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
20ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn (stringthis["name"]; }
21ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifthis["name"= value; }
22ExpandedSubBlockEnd.gif        }

23InBlock.gif        
24ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
25InBlock.gif        /// Test通道集合
26ExpandedSubBlockEnd.gif        /// </summary>

27InBlock.gif        [ConfigurationProperty("channels",IsDefaultCollection = false)]
28InBlock.gif        public ChannelCollection Channels
29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
30InBlock.gif            get 
31ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
32InBlock.gif                ChannelCollection channels = (ChannelCollection)base["channels"];
33InBlock.gif                return channels; 
34InBlock.gif            
35ExpandedSubBlockEnd.gif            }

36InBlock.gif   
37ExpandedSubBlockEnd.gif        }

38InBlock.gif
39ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
40InBlock.gif        /// 密码
41ExpandedSubBlockEnd.gif        /// </summary>

42InBlock.gif        [ConfigurationProperty("password",IsRequired= true)]
43InBlock.gif        public string Password
44ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
45ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn (stringthis["password"]; }
46ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifthis["password"= value; }
47ExpandedSubBlockEnd.gif        }

48InBlock.gif
49InBlock.gif         protected override void DeserializeSection(XmlReader reader)
50ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
51InBlock.gif            base.DeserializeSection(reader);
52ExpandedSubBlockEnd.gif        }

53InBlock.gif
54InBlock.gif        protected override string SerializeSection(ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
55ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
56InBlock.gif            string s = base.SerializeSection(parentElement, name, saveMode);
57InBlock.gif            return s;
58ExpandedSubBlockEnd.gif        }

59ExpandedSubBlockEnd.gif    }

60ExpandedBlockEnd.gif}

61 None.gif
4、Demo
 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.ComponentModel;
 4 None.gif using  System.Data;
 5 None.gif using  System.Drawing;
 6 None.gif using  System.Text;
 7 None.gif using  System.Windows.Forms;
 8 None.gif using  System.Configuration;
 9 None.gif
10 None.gif namespace  Test
11 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
12InBlock.gif    public partial class Demo : Form
13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
14InBlock.gif        public Demo()
15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
16InBlock.gif            InitializeComponent();
17ExpandedSubBlockEnd.gif        }

18InBlock.gif
19InBlock.gif        private void button1_Click(object sender, EventArgs e)
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif            SaveConfig();
22ExpandedSubBlockEnd.gif        }

23InBlock.gif        public void SaveConfig()
24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
25InBlock.gif            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
26InBlock.gif
27ExpandedSubBlockStart.gifContractedSubBlock.gif            Channel[] channel = dot.gif{      new Channel("one","channel_Red"),
28InBlock.gif                                        new Channel("two","channel_Blue"),
29InBlock.gif                                        new Channel("three","channel_Green")
30ExpandedSubBlockEnd.gif                                 }
;
31InBlock.gif
32InBlock.gif            config.Sections.Remove("ClientSection");
33InBlock.gif
34InBlock.gif            ClientSection section = new ClientSection();
35InBlock.gif            section.Name = "服务器1";
36InBlock.gif            section.Password = "123456";
37InBlock.gif            for (int i = 0; i < channel.Length; i++)
38ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
39InBlock.gif                section.Channels.Add(channel[i]);
40ExpandedSubBlockEnd.gif            }

41InBlock.gif            config.Sections.Add("ClientSection", section);
42InBlock.gif            config.Save();
43ExpandedSubBlockEnd.gif        }

44ExpandedSubBlockEnd.gif    }

45ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/xiaowy/archive/2006/08/15/477237.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值