[Version 2.0] XmlSerialization

Apr 4, 2012 at 6:16 PM

We have found a bug with XmlSerialization and IXmlSerializable.  If you have two classes Derived and Base that both are IXmlSerializable and you have an ObservableCollection<Base> the code generated for writing out can look like this:

if (obj is Base) ... write out base

else if (obj is Derived) ... write out derived.

Which means derived classes will be written out with "Base" as the type name.  

 

To get around this we locally modified the code to not use the XmlSerializer and implemented IXmlSerializable on all classes.  

 

This is how we serialize a collection:

 

        private void ReadCollection<T>(XmlReader reader, ObservableCollection<T> observableCollection) where T : LayoutElement
        {
            if (reader.IsEmptyElement)
            {
                reader.Read();
                return;
            }
            reader.MoveToContent();
            reader.Read();
            while (reader.IsStartElement())
            {
                IXmlLayoutContentSerializer serializer = SerializerLocator.Instance.Get(reader.Name);
                T content = serializer.Deserialize(reader) as T;
                observableCollection.Add(content);
            }
            reader.ReadEndElement();
        }