Skip to content
Browse files

Fix XmlSerialization for Document.

  • Loading branch information...
1 parent 59fe535 commit 8562b68ad729af1f2e204d14b3cf39a33cb88987 @lanwin lanwin committed May 22, 2010
Showing with 65 additions and 2 deletions.
  1. +1 −1 source/MongoDB.Tests/UnitTests/TestDocument.cs
  2. +64 −1 source/MongoDB/Document.cs
View
2 source/MongoDB.Tests/UnitTests/TestDocument.cs
@@ -301,7 +301,7 @@ public void CanBeBinarySerialized()
[Test]
public void CanBeXmlSerialized()
{
- var source = new Document("key1", "value1").Add("key2", 10);
+ var source = new Document("key1", "value1").Add("key2", new Document("key", "value").Add("key2", null));
var serializer = new XmlSerializer(typeof(Document));
var writer = new StringWriter();
View
65 source/MongoDB/Document.cs
@@ -2,6 +2,9 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
+using System.Xml;
+using System.Xml.Schema;
+using System.Xml.Serialization;
using MongoDB.Util;
namespace MongoDB
@@ -10,7 +13,7 @@ namespace MongoDB
/// Description of Document.
/// </summary>
[Serializable]
- public class Document : IDictionary<string,object>
+ public class Document : IDictionary<string,object>, IXmlSerializable
{
private readonly List<string> _orderedKeys;
private readonly Dictionary<string,object > _dictionary;
@@ -512,5 +515,65 @@ public Document Merge(Document source)
_orderedKeys.Sort(_keyComparer);
}
+
+ /// <summary>
+ /// 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.
+ /// </summary>
+ /// <returns>
+ /// 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.
+ /// </returns>
+ XmlSchema IXmlSerializable.GetSchema()
+ {
+ return null;
+ }
+
+ /// <summary>
+ /// Generates an object from its XML representation.
+ /// </summary>
+ /// <param name="reader">The <see cref="T:System.Xml.XmlReader"/> stream from which the object is deserialized.</param>
+ void IXmlSerializable.ReadXml(XmlReader reader)
+ {
+ reader.ReadStartElement();
+
+ while(reader.IsStartElement())
+ {
+ var key = reader.Name;
+ object value = null;
+
+ if(reader.MoveToAttribute("type"))
+ {
+ var type = Type.GetType(reader.Value);
+
+ reader.ReadStartElement();
+
+ var serializer = new XmlSerializer(type);
+ value = serializer.Deserialize(reader);
+ }
+ else
+ reader.Read();
+
+ Add(key, value);
+ }
+ }
+
+ /// <summary>
+ /// Converts an object into its XML representation.
+ /// </summary>
+ /// <param name="writer">The <see cref="T:System.Xml.XmlWriter"/> stream to which the object is serialized.</param>
+ void IXmlSerializable.WriteXml(XmlWriter writer)
+ {
+ foreach(var pair in this)
+ {
+ writer.WriteStartElement(pair.Key);
+
+ if(pair.Value == null)
+ continue;
+
+ var type = pair.Value.GetType();
+ writer.WriteAttributeString("type", type.AssemblyQualifiedName);
+ var serializer = new XmlSerializer(type);
+ serializer.Serialize(writer,pair.Value);
+ }
+ }
}
}

0 comments on commit 8562b68

Please sign in to comment.
Something went wrong with that request. Please try again.