IXmlSerializable ReadXml question

The VS2005 Beta 2 MSDN documentation for IXmlSerializable.ReadXml contains the following statement:
"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?

[2024 byte] By [WN] at [2008-2-1]
# 1

Your observation is correct.

WriteXml should not read the wrapper element. It should only write the contents (attributes, contents, child elements).

ReadXml must read the wrapper element, including all of its contents.

I'll open a doc bug to update the documentation.

EugeneOsovetsky at 2007-9-8 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 2
Thanks. Are there good reasons as to why ReadXml needs to consume the wrapper element, even though WriteXml did not originally emit it? On the surface, it seems a bit odd, so I'm curious as to why it was designed this way.
WN at 2007-9-8 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 3

ReadStartElement assumes that reader already positioned on element. And ReadXml must advance reader to reach “FirstName” element first. Instead of

//reader.ReadStartElement();

// reader.ReadEndElement();

I would suggest

if(!reader.IsEmptyElement && reader.Read())

{

reader.ReadEndElement();

}

Another reason is XmlAttributeAttribute, e.g.


public class Person

{

[XmlAttribute()]

int id;

string Name;

}

That way ReadXml allows structures to deserialize it’s attributes.

TheXMLMan at 2007-9-8 > top of Msdn Tech,.NET Development,XML and the .NET Framework...

.NET Development

Site Classified