IXmlSerializable ReadXml question
"When this method is called, the reader stream is positioned at exactly the point that the WriteXml method began writing. Typically, this is immediately after the start tag that indicates the beginning of your serialized object. When this method returns, it must have consumed exactly those nodes that were written by the WriteXml method, so that the reader stream is positioned where that method left it."
From my own tests, it seems that the reader stream is positioned one node prior to the expected start position. Here's an example, given the following WriteXml implementation.
void IXmlSerializable.WriteXml(XmlWriter writer)
{
writer.WriteStartElement("FirstName");
writer.WriteString(FirstName);
writer.WriteEndElement();
writer.WriteStartElement("LastName");
writer.WriteString(LastName);
writer.WriteEndElement();
}
If the ReadXml method is implemented as follows, it will fail, unless the commented-out lines are uncommented.
void IXmlSerializable.ReadXml(XmlReader reader)
{
//reader.ReadStartElement();
reader.ReadStartElement("FirstName");
FirstName = reader.ReadContentAsString();
reader.ReadEndElement();
reader.ReadStartElement("LastName");
LastName = reader.ReadContentAsString();
reader.ReadEndElement();
//reader.ReadEndElement();
}
Since WriteXml only emits "FirstName" and "LastName" elements, it would seem that ReadXml should only need to consume "FirstName" and "LastName" elements, without having to consume some sort of "parent" element.
At the VS2005 Product Feedback site, there seems to be a bug report (FDBK11658) that appears to be related to this behaviour. However, it was closed as being "by design", and with a rather vague explanation. Can anyone clarify exactly which elements IXmlSerializable's WriteXml and ReadXml should emit and consume, respectively?

