Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix XmlSerialization for MongoSymbol.

  • Loading branch information...
commit 9fbba0fecca9cf753dcb06a00401dcefdb1c6cc3 1 parent 922b3d8
@lanwin lanwin authored
View
13 source/MongoDB.Tests/UnitTests/TestMongoSymbol.cs
@@ -147,5 +147,18 @@ public void CanBeXmlSerialized()
Assert.AreEqual(source, dest);
}
+
+ [Test]
+ public void CanBeXmlSerializedWhenValueNull()
+ {
+ var source = new MongoSymbol(null);
+ var serializer = new XmlSerializer(typeof(MongoSymbol));
+
+ var writer = new StringWriter();
+ serializer.Serialize(writer, source);
+ var dest = (MongoSymbol)serializer.Deserialize(new StringReader(writer.ToString()));
+
+ Assert.AreEqual(source, dest);
+ }
}
}
View
38 source/MongoDB/MongoSymbol.cs
@@ -1,4 +1,7 @@
using System;
+using System.Xml;
+using System.Xml.Schema;
+using System.Xml.Serialization;
namespace MongoDB
{
@@ -6,7 +9,7 @@ namespace MongoDB
/// Type to hold an interned string that maps to the bson symbol type.
/// </summary>
[Serializable]
- public struct MongoSymbol : IEquatable<MongoSymbol>, IEquatable<String>, IComparable<MongoSymbol>, IComparable<String>
+ public struct MongoSymbol : IEquatable<MongoSymbol>, IEquatable<String>, IComparable<MongoSymbol>, IComparable<String>, IXmlSerializable
{
/// <summary>
/// Gets or sets the empty.
@@ -232,5 +235,38 @@ public MongoSymbol(string value)
public override int GetHashCode(){
return (Value != null ? Value.GetHashCode() : 0);
}
+
+ /// <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)
+ {
+ if(reader.IsEmptyElement)
+ return;
+
+ Value = string.Intern(reader.ReadString());
+ }
+
+ /// <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)
+ {
+ if(Value != null)
+ writer.WriteString(Value);
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.