SerializableXMLDictionary.cs 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Serialization;
  6. #pragma warning disable CS8602 // Dereference of a possibly null reference.
  7. #pragma warning disable CS8603 // Possible null reference return.
  8. #pragma warning disable CS8604 // Possible null reference argument.
  9. #pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
  10. namespace EasyDevCore.Common.Wrapper
  11. {
  12. /// <summary>
  13. ///
  14. /// </summary>
  15. /// <typeparam name="TKey">The type of the key.</typeparam>
  16. /// <typeparam name="TValue">The type of the value.</typeparam>
  17. [XmlRoot("Dictionary")]
  18. #pragma warning disable CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
  19. public class SerializableXMLDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
  20. #pragma warning restore CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
  21. {
  22. #region IXmlSerializable Members
  23. /// <summary>
  24. /// This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return null (Nothing in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the <see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute" /> to the class.
  25. /// </summary>
  26. /// <returns>
  27. /// An <see cref="T:System.Xml.Schema.XmlSchema" /> that describes the XML representation of the object that is produced by the <see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)" /> method and consumed by the <see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)" /> method.
  28. /// </returns>
  29. public System.Xml.Schema.XmlSchema GetSchema()
  30. {
  31. return null;
  32. }
  33. /// <summary>
  34. /// Generates an object from its XML representation.
  35. /// </summary>
  36. /// <param name="reader">The <see cref="T:System.Xml.XmlReader" /> stream from which the object is deserialized.</param>
  37. public void ReadXml(System.Xml.XmlReader reader)
  38. {
  39. XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
  40. XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
  41. bool wasEmpty = reader.IsEmptyElement;
  42. reader.Read();
  43. if (wasEmpty)
  44. return;
  45. while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
  46. {
  47. reader.ReadStartElement("item");
  48. reader.ReadStartElement("key");
  49. TKey key = (TKey)keySerializer.Deserialize(reader);
  50. reader.ReadEndElement();
  51. reader.ReadStartElement("value");
  52. TValue value = (TValue)valueSerializer.Deserialize(reader);
  53. reader.ReadEndElement();
  54. this.Add(key, value);
  55. reader.ReadEndElement();
  56. reader.MoveToContent();
  57. }
  58. reader.ReadEndElement();
  59. }
  60. /// <summary>
  61. /// Converts an object into its XML representation.
  62. /// </summary>
  63. /// <param name="writer">The <see cref="T:System.Xml.XmlWriter" /> stream to which the object is serialized.</param>
  64. public void WriteXml(System.Xml.XmlWriter writer)
  65. {
  66. XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
  67. XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
  68. foreach (TKey key in this.Keys)
  69. {
  70. writer.WriteStartElement("item");
  71. writer.WriteStartElement("key");
  72. keySerializer.Serialize(writer, key);
  73. writer.WriteEndElement();
  74. writer.WriteStartElement("value");
  75. TValue value = this[key];
  76. valueSerializer.Serialize(writer, value);
  77. writer.WriteEndElement();
  78. writer.WriteEndElement();
  79. }
  80. }
  81. #endregion
  82. }
  83. }