views:

7154

answers:

3

Once a programmer decides to implement IXmlSerializable, what are the rules and best practices for implementing it? I've heard that GetSchema() should return null and ReadXml should move to the next element before returning. Are these true? And what about WriteXml: should it write a root element for the object or is it assumed that the root is already written? How should child objects be treated and written.

Here's a sample of what I have now. I'll update it as I get good responses.

public class Calendar: IEnumerable<Gvent>, IXmlSerializable
{
    public XmlSchema GetSchema() { return null; }

    public void ReadXml(XmlReader reader)
    {
        if (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "Calendar")
        {
            _Name = reader["Name"];
            _Enabled = Boolean.Parse(reader["Enabled"]);
            _Color = Color.FromArgb(Int32.Parse(reader["Color"]));

            if (reader.ReadToDescendant("Event"))
            {
                while (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "Event")
                {
                    var evt = new Event();
                    evt.ReadXml(reader);
                    _Events.Add(evt);
                }
            }
            reader.Read();
        }
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteAttributeString("Name", _Name);
        writer.WriteAttributeString("Enabled", _Enabled.ToString());
        writer.WriteAttributeString("Color", _Color.ToArgb().ToString());

        foreach (var evt in _Events)
        {
            writer.WriteStartElement("Event");
            evt.WriteXml(writer);
            writer.WriteEndElement();
        }
    }
}

public class Event : IXmlSerializable
{
    public XmlSchema GetSchema() { return null; }

    public void ReadXml(XmlReader reader)
    {
        if (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "Event")
        {
            _Title = reader["Title"];
            _Start = DateTime.FromBinary(Int64.Parse(reader["Start"]));
            _Stop = DateTime.FromBinary(Int64.Parse(reader["Stop"]));
            reader.Read();
        }
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteAttributeString("Title", _Title);
        writer.WriteAttributeString("Start", _Start.ToBinary().ToString());
        writer.WriteAttributeString("Stop", _Stop.ToBinary().ToString());
    }
}
+21  A: 

Yes, GetSchema() should return null.

IXmlSerializable.GetSchema Method This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return a null reference (Nothing in Visual Basic) (Nothing in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the XmlSchemaProviderAttribute to the class.

For both read and write, the object element has already been written, so you don't need to add an outer element in write. For example, you can just start reading/writing attributes in the two.

For write:

The WriteXml implementation you provide should write out the XML representation of the object. The framework writes a wrapper element and positions the XML writer after its start. Your implementation may write its contents, including child elements. The framework then closes the wrapper element.

And for read:

The ReadXml method must reconstitute your object using the information that was written by the WriteXml method.

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. Unlike the WriteXml method, the framework does not handle the wrapper element automatically. Your implementation must do so. Failing to observe these positioning rules may cause code to generate unexpected runtime exceptions or corrupt data.

I'll agree that is a little unclear, but it boils down to "it is your job to Read() the end-element tag of the wrapper".

Marc Gravell
What about writing out and reading the Event elements? It feels hackish to manually write the starting element. I think I've seen someone use an XmlSerializer in the write method to write each child element.
Greg
@Greg; either usage is fine... yes, you can use a nested XmlSerializer if you need, but it isn't the only option.
Marc Gravell
Thanks for these precisions, the sample code inside MSDN is pretty unuseful and unclear regarding this. I got stuck many times and was wondering about the asymmetric behavior of Read/WriteXml.
jdehaan
@Mark, could you please help me out: http://stackoverflow.com/questions/2665041
Shimmy
+6  A: 

I wrote one article on the subject with samples as the MSDN documentation is by now quite unclear and the examples you can find on the web are most of the time incorrectly implemented.

Pitfalls are handling of locales and empty elements beside what Marc Gravell already mentioned.

http://www.codeproject.com/KB/XML/ImplementIXmlSerializable.aspx

jdehaan
Excellent article! I'll definitely reference it next time I'm looking to serialize some data.
Greg
Thanks! the amount of positive feedback rewards the amount of time invested in writing it. I deeply appreciate that you like it! Do not hesitate to ask to criticize some points.
jdehaan
+3  A: 

Yes, the whole thing is a bit of a minefield, isn't it? Marc Gravell's answer pretty much covers it, but I'd like to add that in a project I worked on we found it quite awkward to have to manually write the outer XML element. It also resulted in inconsistent XML element names for objects of the same type.

Our solution was to define our own IXmlSerializable interface, derived from the system one, which added a method called WriteOuterXml(). As you can guess, this method would simply write the outer element, then call WriteXml(), then write the end of the element. Of course, the system XML serializer wouldn't call this method, so it was only useful when we did our own serialization, so that may or may not be helpful in your case. Similarly, we added a ReadContentXml() method, which didn't read the outer element, only its content.

Evgeny
With C# 3.0 you can probably do this by writing an extension method instead, but an interesting idea.
Marc Gravell
Yes, good point - this was in the days of .NET 1.1
Evgeny