Created
December 14, 2015 07:38
-
-
Save tupunco/aeed7b4dd88db92ba092 to your computer and use it in GitHub Desktop.
XML Serializable Collections, 可 XML 序列化的 字典/HashSet 类型
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Runtime.Serialization; | |
using System.Xml; | |
using System.Xml.Schema; | |
using System.Xml.Serialization; | |
namespace Tup.Utilities | |
{ | |
/// <summary> | |
/// 支持XML序列化的泛型Dictionary类 | |
/// http://www.cnblogs.com/facebuyer/archive/2009/05/04/1448511.html | |
/// </summary> | |
/// <typeparam name="TKey"></typeparam> | |
/// <typeparam name="TValue"></typeparam> | |
[XmlRoot("dictionary")] | |
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable | |
{ | |
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) | |
{ | |
} | |
#region IXmlSerializable Members | |
public XmlSchema GetSchema() | |
{ | |
return null; | |
} | |
/// <summary> | |
/// 从对象的XML表示形式生成该对象 | |
/// </summary> | |
/// <param name="reader"></param> | |
public void ReadXml(System.Xml.XmlReader reader) | |
{ | |
var keySerializer = new XmlSerializer(typeof(TKey)); | |
var valueSerializer = new XmlSerializer(typeof(TValue)); | |
if (reader.IsEmptyElement || !reader.Read()) | |
{ | |
return; | |
} | |
while (reader.NodeType != XmlNodeType.EndElement) | |
{ | |
reader.ReadStartElement("item"); //cjb:2009-05-04 | |
//reader.ReadStartElement("key"); | |
TKey key = (TKey)keySerializer.Deserialize(reader); | |
//reader.ReadEndElement(); | |
//reader.ReadStartElement("value"); | |
TValue value = (TValue)valueSerializer.Deserialize(reader); | |
//reader.ReadEndElement(); | |
reader.ReadEndElement(); | |
this.Add(key, value); | |
reader.MoveToContent(); | |
} | |
reader.ReadEndElement(); | |
} | |
/// <summary> | |
/// 将对象转换为其XML表示形式 | |
/// </summary> | |
/// <param name="writer"></param> | |
public void WriteXml(System.Xml.XmlWriter writer) | |
{ | |
var keySerializer = new XmlSerializer(typeof(TKey)); | |
var 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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Runtime.Serialization; | |
using System.Xml; | |
using System.Xml.Schema; | |
using System.Xml.Serialization; | |
namespace Tup.Utilities | |
{ | |
/// <summary> | |
/// 支持XML序列化的泛型 HashSet 类 | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
[XmlRoot("HashSet")] | |
public class SerializableHashSet<T> : HashSet<T>, IXmlSerializable | |
{ | |
#region 构造函数 | |
/// <summary> | |
/// | |
/// </summary> | |
public SerializableHashSet() : base() { } | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="collection"></param> | |
public SerializableHashSet(IEnumerable<T> collection) : base(collection) { } | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="comparer"></param> | |
public SerializableHashSet(IEqualityComparer<T> comparer) : base(comparer) { } | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="collection"></param> | |
/// <param name="comparer"></param> | |
public SerializableHashSet(IEnumerable<T> collection, IEqualityComparer<T> comparer) : base(collection, comparer) { } | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="info"></param> | |
/// <param name="context"></param> | |
protected SerializableHashSet(SerializationInfo info, StreamingContext context) : base(info, context) { } | |
#endregion | |
#region IXmlSerializable 成员 | |
/// <summary> | |
/// | |
/// </summary> | |
/// <returns></returns> | |
public XmlSchema GetSchema() | |
{ | |
return null; | |
} | |
/// <summary> | |
/// 从对象的XML表示形式生成该对象 | |
/// </summary> | |
/// <param name="reader"></param> | |
public void ReadXml(System.Xml.XmlReader reader) | |
{ | |
var keySerializer = new XmlSerializer(typeof(T)); | |
if (reader.IsEmptyElement || !reader.Read()) | |
return; | |
while (reader.NodeType != XmlNodeType.EndElement) | |
{ | |
//reader.ReadStartElement("item"); | |
T key = (T)keySerializer.Deserialize(reader); | |
//reader.ReadEndElement(); | |
this.Add(key); | |
reader.MoveToContent(); | |
} | |
reader.ReadEndElement(); | |
} | |
/// <summary> | |
/// 将对象转换为其XML表示形式 | |
/// </summary> | |
/// <param name="writer"></param> | |
public void WriteXml(System.Xml.XmlWriter writer) | |
{ | |
var keySerializer = new XmlSerializer(typeof(T)); | |
foreach (T key in this) | |
{ | |
//writer.WriteStartElement("item"); | |
keySerializer.Serialize(writer, key); | |
//writer.WriteEndElement(); | |
} | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment