<ComponentSettings>
<ComponentOne>
<SettingOne>SettingOneValue</SettingOne>
<SettingTwo>SettingTwoValue</SettingTwo>
</ComponentOne>
<ComponentTwo>
<AnotherSettingOne>AnotherSettingOneValue</AnotherSettingOne>
<AnotherSettingTwo>AnotherSettingTwoValue</AnotherSettingTwo>
</ComponentTwo>
</ComponentSettings>
Dictionary本身支援Serialization。key,value都是Serializable
不能直接xml序列化, 轉化成list再序列化
可以從Dictionary泛型類派生並實現IXmlSerializable
-
C# code
-
public class ComponentsProperties : Dictionary<string, Dictionary<string,string>>, System.Xml.Serialization.IXmlSerializable{ #region IXmlSerializable Members public System.Xml.Schema.XmlSchema GetSchema() { throw new NotImplementedException(); } public void ReadXml(System.Xml.XmlReader reader) { if (reader.IsEmptyElement) return; reader.Read(); while (reader.NodeType != System.Xml.XmlNodeType.EndElement) { string key = reader.Name; this[key] = new Dictionary<string, string>(); if (!reader.IsEmptyElement) { reader.ReadStartElement(); while (reader.NodeType != System.Xml.XmlNodeType.EndElement) this[key][reader.Name] = reader.ReadElementString(); } reader.Read(); } } public void WriteXml(System.Xml.XmlWriter writer) { foreach (string key in this.Keys) { writer.WriteStartElement(key); foreach (string key1 in this[key].Keys) writer.WriteElementString(key1, this[key][key1]); writer.WriteEndElement(); } } #endregion}
調用時候的寫法和原來是一樣的,例如:
-
C# code
-
public class TestClass{ [XmlElement("ComponentSettins")] public ComponentsProperties Settings { get; set; }}void DoTest(){ TestClass test = new TestClass(); test.Settings = new ComponentsProperties(); test.Settings["ComponentOne"] = new Dictionary<string, string>(); test.Settings["ComponentOne"]["SettingOne"] = "SettingOneValue"; test.Settings["ComponentOne"]["SettingTwo"] = "SettingTwoValue"; test.Settings["ComponentTwo"] = new Dictionary<string, string>(); test.Settings["ComponentTwo"]["AnotherSettingOne"] = "AnotherSettingOneValue"; test.Settings["ComponentTwo"]["AnotherSettingTwo"] = "AnotherSettingTwoValue"; XmlSerializer xs = new XmlSerializer(typeof(TestClass)); FileStream fs = new FileStream(@"d:/test.xml", FileMode.Create); xs.Serialize(fs, test); fs.Close();}
==========================================================================view plaincopy to clipboardprint?
- /// <summary>
- /// 支援XML序列化的泛型 Dictionary
- /// </summary>
- /// <typeparam name="TKey"></typeparam>
- /// <typeparam name="TValue"></typeparam>
- [XmlRoot("SerializableDictionary")]
- public class SerializableDictionary<TKey, TValue>
- : Dictionary<TKey, TValue>, IXmlSerializable
- {
-
- #region 建構函式
- public SerializableDictionary()
- : base()
- {
- }
- public SerializableDictionary(IDictionary<TKey, TValue> dictionary)
- : base(dictionary)
- {
- }
-
- public SerializableDictionary(IEqualityComparer<TKey> comparer)
- : base(comparer)
- {
- }
-
- public SerializableDictionary(int capacity)
- : base(capacity)
- {
- }
- public SerializableDictionary(int capacity, IEqualityComparer<TKey> comparer)
- : base(capacity, comparer)
- {
- }
- protected SerializableDictionary(SerializationInfo info, StreamingContext context)
- : base(info, context)
- {
- }
- #endregion
- #region IXmlSerializable Members
- public System.Xml.Schema.XmlSchema GetSchema()
- {
- return null;
- }
- /// <summary>
- /// 從對象的 XML 表示形式產生該對象
- /// </summary>
- /// <param name="reader"></param>
- public void ReadXml(System.Xml.XmlReader reader)
- {
- XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
- XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
- bool wasEmpty = reader.IsEmptyElement;
- reader.Read();
- if (wasEmpty)
- return;
- while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
- {
- reader.ReadStartElement("item");
- reader.ReadStartElement("key");
- TKey key = (TKey)keySerializer.Deserialize(reader);
- reader.ReadEndElement();
- reader.ReadStartElement("value");
- TValue value = (TValue)valueSerializer.Deserialize(reader);
- reader.ReadEndElement();
- this.Add(key, value);
- reader.ReadEndElement();
- reader.MoveToContent();
- }
- reader.ReadEndElement();
- }
-
- /**/
- /// <summary>
- /// 將對象轉換為其 XML 表示形式
- /// </summary>
- /// <param name="writer"></param>
- public void WriteXml(System.Xml.XmlWriter writer)
- {
- XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
- XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
- foreach (TKey key in this.Keys)
- {
- writer.WriteStartElement("item");
- writer.WriteStartElement("key");
- keySerializer.Serialize(writer, key);
- writer.WriteEndElement();
- writer.WriteStartElement("value");
- TValue value = this[key];
- valueSerializer.Serialize(writer, value);
- writer.WriteEndElement();
- writer.WriteEndElement();
- }
- }
- #endregion
- }