Posted By

dzr_gregory on 10/22/10


Tagged

dictionary serializable IXmlSerializable SerializableDictionary


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Tyster


SerializableDictionary using IXmlSerializable


 / Published in: C#
 

URL: http://i-liger.com/article/xml-serializatsiya-generic-dictionary

Original posted here: http://i-liger.com/article/xml-serializatsiya-generic-dictionary

  1. [XmlRoot("dictionary")]
  2. public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
  3. {
  4. public XmlSchema GetSchema()
  5. {
  6. return null;
  7. }
  8.  
  9. public void ReadXml(XmlReader xmlReader)
  10. {
  11. XmlSerializer keyXmlSerializer = new XmlSerializer(typeof(TKey));
  12. XmlSerializer valueXmlSerializer = new XmlSerializer(typeof(TValue));
  13.  
  14. if (xmlReader.IsEmptyElement)
  15. return;
  16.  
  17. xmlReader.ReadStartElement("root");
  18.  
  19. while (xmlReader.NodeType != XmlNodeType.EndElement)
  20. {
  21. xmlReader.ReadStartElement("item");
  22. xmlReader.ReadStartElement("key");
  23.  
  24. TKey key = (TKey)keyXmlSerializer.Deserialize(xmlReader);
  25.  
  26. xmlReader.ReadEndElement();
  27. xmlReader.ReadStartElement("value");
  28. TValue value = (TValue)valueXmlSerializer.Deserialize(xmlReader);
  29. xmlReader.ReadEndElement();
  30.  
  31. this.Add(key, value);
  32. xmlReader.ReadEndElement();
  33. }
  34.  
  35. xmlReader.ReadEndElement();
  36. }
  37.  
  38. public SerializableDictionary<TKey, TValue> ReadFromString(string xmlString)
  39. {
  40. SerializableDictionary<TKey, TValue> result = null;
  41.  
  42. StringReader stringReader = new StringReader(xmlString);
  43. XmlTextReader xmlTextReader = new XmlTextReader(stringReader);
  44.  
  45. ReadXml(xmlTextReader);
  46.  
  47. xmlTextReader.Close();
  48. stringReader.Close();
  49.  
  50. return result;
  51. }
  52.  
  53. public void WriteXml(XmlWriter xmlWriter)
  54. {
  55. XmlSerializer keyXMLSerializer = new XmlSerializer(typeof(TKey));
  56. XmlSerializer valueXMLSerializer = new XmlSerializer(typeof(TValue));
  57.  
  58. foreach (TKey key in this.Keys)
  59. {
  60. xmlWriter.WriteStartElement("item");
  61.  
  62. xmlWriter.WriteStartElement("key");
  63. keyXMLSerializer.Serialize(xmlWriter, key);
  64. xmlWriter.WriteEndElement();
  65.  
  66. xmlWriter.WriteStartElement("value");
  67. TValue value = this[key];
  68. valueXMLSerializer.Serialize(xmlWriter, value);
  69. xmlWriter.WriteEndElement();
  70.  
  71. xmlWriter.WriteEndElement();
  72. }
  73. }
  74.  
  75. public string SerializeToString()
  76. {
  77. string result = null;
  78. MemoryStream memoryStream = new MemoryStream();
  79. XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
  80. xmlTextWriter.Namespaces = true;
  81.  
  82. WriteXml(xmlTextWriter);
  83.  
  84. xmlTextWriter.Close();
  85. memoryStream.Close();
  86.  
  87. result = Encoding.UTF8.GetString(memoryStream.GetBuffer());
  88. result = result.Substring(result.IndexOf(Convert.ToChar(60)));
  89. result = result.Substring(0, (result.LastIndexOf(Convert.ToChar(62)) + 1));
  90. result = "<root>" + result + "</root>";
  91.  
  92. return result;
  93. }
  94. }

Report this snippet  

You need to login to post a comment.