1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public class SerizlizerDictionary<Tkey, TValue> : Dictionary<Tkey, TValue>, IXmlSerializable { public XmlSchema GetSchema() { return null; } public void ReadXml(XmlReader reader) { XmlSerializer keySer = new XmlSerializer(typeof(Tkey)); XmlSerializer valueSer = new XmlSerializer(typeof(TValue)); reader.Read(); while (reader.NodeType != XmlNodeType.EndElement) { Tkey key = (Tkey)keySer.Deserialize(reader); TValue value = (TValue)valueSer.Deserialize(reader); this.Add(key, value); } reader.Read(); } public void WriteXml(XmlWriter writer) { XmlSerializer keySer = new XmlSerializer(typeof(Tkey)); XmlSerializer valueSer = new XmlSerializer(typeof(TValue));
foreach(KeyValuePair<Tkey, TValue> kv in this) { keySer.Serialize(writer, kv.Key); valueSer.Serialize(writer, kv.Value); } } }
|