I belive your xml serialization tests are too isolated.
If you have any other elements before or after in the xml
document, your ReadXml() method mis-behaves if the
serialized type was null.
The contract for IXmlReadSerializable.ReadXml says:
When this method is called, the reader is positioned at
the start of the element that wraps the information for
your type. That is, just before the start tag that indicates
the beginning of a serialized object. When this method
returns, it must have read the entire element from
beginning to end, including all of its contents.
Quoted from msdn2.microsoft.com.
When the type is null, you are not advancing the
XmlReader past the contents of the element.
Here is a trivial example which fails:
XmlSerializer ser = new
XmlSerializer(typeof(NullableDateTime));
NullableDateTime ndt = new NullableDateTime();
XmlTextWriter writer = new XmlTextWriter(@"test.xml",
Encoding.UTF8);
writer.WriteStartElement("Foobar");
ser.Serialize(writer, ndt);
writer.WriteEndElement();
writer.Close();
XmlTextReader reader = new XmlTextReader(new
StreamReader(@"test.xml", Encoding.UTF8));
reader.ReadStartElement("Foobar");
NullableDateTime newObj = (NullableDateTime) ser.
Deserialize(reader);
reader.ReadEndElement();
reader.Close();
Assert.AreEqual(ndt, newObj);
Logged In: NO
Adding these two lines to every ReadXml() method fixes the
problem:
if (reader.IsEmptyElement)
reader.Read();
Logged In: YES
user_id=742947
Thanks for submiting this bug report.
I'm trying to reproduce the bug, can you tell me the version of
the .NET Framework and of NullableTypes are you using?