序列化Dictionary属性到XML里
来源:互联网 发布:linux hex编辑器 编辑:程序博客网 时间:2024/05/21 11:16
<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();}==========================================================================
- /// <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
- }
- 序列化Dictionary属性到XML里
- c# 支持XML序列化的泛型 Dictionary
- c# 支持XML序列化的泛型 Dictionary
- Dictionary序列化
- xml文件的序列化(信息从内存写到文件里)存贮文件
- C#里 如何序列化与反序列化 hashtable Dictionary及C#连接mysql数据库
- unity Dictionary序列化和反序列化及XML本地数据存储
- 图像文件序列化到XML文件中
- 从TabletoList到Xml、Json序列化
- C# 序列化xml数据到class
- 简述Xml.Serialization如何序列化对象到XML文件
- xml序列化到本地以及使用Pull解析xml
- 将java类对象序列化到文件里
- 序列化及反序列化Dictionary
- C# Dictionary序列化/反序列化
- C# Dictionary序列化/反序列化
- C# 解决Dictionary不能序列化
- Dictionary转Json(序列化)
- Android 屏幕保护程序制作及源码
- 判断窗体是否已经打开
- 回家过年(深圳--南昌)
- JAVA高级视频02_IO输入与输出 05 FileInputStream和FileOutPutStream
- 我爱 Ruby 的三十七个理由
- 序列化Dictionary属性到XML里
- 初步oracle数据库中建立用户建立表
- 22.Chain Of Responsibility(行为型模式)
- 图片压缩的JAVAScript
- 学习笔记(一)-Linq To Sql&PhotoShop第二章
- 学习笔记(二)-LINQ to Objects&PS第三章
- 程序的设计什么时候用户与设计者没有差别就好了
- Invoke and BeginInvoke BeginInvoke和EndInvoke方法 (转)
- ql语言艺术(4) SQL的本质