Mercurial > pub > bltoolkit
comparison Demo/WebServices/ObjectModel/XmlMap.cs @ 0:f990fcb411a9
Копия текущей версии из github
| author | cin |
|---|---|
| date | Thu, 27 Mar 2014 21:46:09 +0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:f990fcb411a9 |
|---|---|
| 1 using System; | |
| 2 using System.Collections.Generic; | |
| 3 using System.Xml; | |
| 4 using System.Xml.Schema; | |
| 5 using System.Xml.Serialization; | |
| 6 | |
| 7 using BLToolkit.TypeBuilder; | |
| 8 | |
| 9 namespace Demo.WebServices.ObjectModel | |
| 10 { | |
| 11 [Serializable] | |
| 12 public class XmlMap<TKey, TValue>: Dictionary<TKey, TValue>, IXmlSerializable | |
| 13 { | |
| 14 private const string KEY_NAME = "k"; | |
| 15 private const string VALUE_NAME = "v"; | |
| 16 | |
| 17 private static readonly XmlSerializer<TKey> _keySerializer = new XmlSerializer<TKey>(KEY_NAME); | |
| 18 private static readonly XmlSerializer<TValue> _valueSerializer = new XmlSerializer<TValue>(VALUE_NAME); | |
| 19 | |
| 20 #region IXmlSerializable members | |
| 21 | |
| 22 XmlSchema IXmlSerializable.GetSchema() | |
| 23 { | |
| 24 return null; | |
| 25 } | |
| 26 | |
| 27 void IXmlSerializable.ReadXml(XmlReader reader) | |
| 28 { | |
| 29 if (!reader.ReadToDescendant(KEY_NAME)) | |
| 30 { | |
| 31 reader.Skip(); | |
| 32 return; | |
| 33 } | |
| 34 | |
| 35 do | |
| 36 { | |
| 37 Add(_keySerializer.Deserialize(reader), | |
| 38 _valueSerializer.Deserialize(reader)); | |
| 39 } | |
| 40 while (reader.NodeType != XmlNodeType.EndElement); | |
| 41 | |
| 42 reader.ReadEndElement(); | |
| 43 } | |
| 44 | |
| 45 void IXmlSerializable.WriteXml(XmlWriter writer) | |
| 46 { | |
| 47 foreach (KeyValuePair<TKey, TValue> pair in this) | |
| 48 { | |
| 49 _keySerializer .Serialize(writer, pair.Key); | |
| 50 _valueSerializer.Serialize(writer, pair.Value); | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 #endregion | |
| 55 | |
| 56 private class XmlSerializer<T> | |
| 57 { | |
| 58 private readonly XmlSerializer _instance; | |
| 59 | |
| 60 public XmlSerializer(string elementName) | |
| 61 { | |
| 62 _instance = new XmlSerializer( | |
| 63 TypeFactory.GetType(typeof(T)), new XmlRootAttribute(elementName)); | |
| 64 } | |
| 65 | |
| 66 public void Serialize(XmlWriter writer, T value) | |
| 67 { | |
| 68 _instance.Serialize(writer, value); | |
| 69 } | |
| 70 | |
| 71 public T Deserialize(XmlReader reader) | |
| 72 { | |
| 73 return (T)_instance.Deserialize(reader); | |
| 74 } | |
| 75 } | |
| 76 } | |
| 77 } |
