corrade-vassal – Blame information for rev 13
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
13 | eva | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
||
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
||
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
||
5 | /////////////////////////////////////////////////////////////////////////// |
||
6 | |||
7 | using System.Collections.Generic; |
||
8 | using System.Xml; |
||
9 | using System.Xml.Schema; |
||
10 | using System.Xml.Serialization; |
||
11 | |||
12 | namespace wasSharp |
||
13 | { |
||
14 | public class Collections |
||
15 | { |
||
16 | /// <summary> |
||
17 | /// A serializable dictionary class. |
||
18 | /// </summary> |
||
19 | /// <typeparam name="TKey">the key</typeparam> |
||
20 | /// <typeparam name="TValue">the value</typeparam> |
||
21 | [XmlRoot("Dictionary")] |
||
22 | public class SerializableDictionary<TKey, TValue> |
||
23 | : Dictionary<TKey, TValue>, IXmlSerializable |
||
24 | { |
||
25 | #region IXmlSerializable Members |
||
26 | |||
27 | public XmlSchema GetSchema() |
||
28 | { |
||
29 | return null; |
||
30 | } |
||
31 | |||
32 | public void ReadXml(XmlReader reader) |
||
33 | { |
||
34 | XmlSerializer keySerializer = new XmlSerializer(typeof (TKey)); |
||
35 | XmlSerializer valueSerializer = new XmlSerializer(typeof (TValue)); |
||
36 | |||
37 | bool wasEmpty = reader.IsEmptyElement; |
||
38 | reader.Read(); |
||
39 | |||
40 | if (wasEmpty) |
||
41 | return; |
||
42 | |||
43 | while (reader.NodeType != XmlNodeType.EndElement) |
||
44 | { |
||
45 | reader.ReadStartElement("Item"); |
||
46 | |||
47 | reader.ReadStartElement("Key"); |
||
48 | TKey key = (TKey) keySerializer.Deserialize(reader); |
||
49 | reader.ReadEndElement(); |
||
50 | |||
51 | reader.ReadStartElement("Value"); |
||
52 | TValue value = (TValue) valueSerializer.Deserialize(reader); |
||
53 | reader.ReadEndElement(); |
||
54 | |||
55 | Add(key, value); |
||
56 | |||
57 | reader.ReadEndElement(); |
||
58 | reader.MoveToContent(); |
||
59 | } |
||
60 | reader.ReadEndElement(); |
||
61 | } |
||
62 | |||
63 | public void WriteXml(XmlWriter writer) |
||
64 | { |
||
65 | XmlSerializer keySerializer = new XmlSerializer(typeof (TKey)); |
||
66 | XmlSerializer valueSerializer = new XmlSerializer(typeof (TValue)); |
||
67 | |||
68 | foreach (TKey key in Keys) |
||
69 | { |
||
70 | writer.WriteStartElement("Item"); |
||
71 | |||
72 | writer.WriteStartElement("Key"); |
||
73 | keySerializer.Serialize(writer, key); |
||
74 | writer.WriteEndElement(); |
||
75 | |||
76 | writer.WriteStartElement("Value"); |
||
77 | TValue value = this[key]; |
||
78 | valueSerializer.Serialize(writer, value); |
||
79 | writer.WriteEndElement(); |
||
80 | |||
81 | writer.WriteEndElement(); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | #endregion |
||
86 | } |
||
87 | } |
||
88 | } |