1

I've base class and few derived classes adding properties to the base class

Until now, I've always serialized and deserialized lists full with mixed objects of derived classes, and the only thing I'd to do it to add the [XmlInclude(typeof(child))] and C# would take care of the rest.

This time I want to serialize/deserialize object of derived class directly rather than putting it in list. The most interesting part is that when I serialize the object while it is in list the generated xml is different from "direct" serialization. Furthermore - I can't deserialize the directly serlized-object to instance of it's base class item, while the list deserializes just fine (I've added an example below)

This the code of the sample classes:

    [XmlInclude(typeof(child))]
    public abstract class father 
    { ... }

    public class child : father
    {
        public double Sum { get; set; }
        public char Currency { get; set; }
    }

And here is the init code for the examples below:

 father oD = new child() { Currency = '$', Sum = 1 };
 string xml_str = Tools.Serialize(oD);

The XML output is:

<child xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Sum>1</Sum><Currency>36</Currency></child>

When I put the object in a list and serialize the list, the ouput is

<ArrayOfFather xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><father xsi:type="child"><Sum>1</Sum><Currency>36</Currency></father></ArrayOfFather>

as you can see the XML is different:

<father xsi:type="child"><Sum>1</Sum><Currency>36</Currency></father>

The problem is that while the following two lines work (as I am providing the child-class as deserialization target):

 child oD1 = Tools.DeSerializeGeneric<child>(xml_str);
 father oD2 = Tools.DeSerializeGeneric<child>(xml_str);

I can't make the third one (and the one I need) to deserialize

 father oD4 = Tools.DeSerializeGeneric<father>(xml_str);

Throws

System.InvalidOperationException: There is an error in XML document (1, 2). ---> System.InvalidOperationException: <child xmlns=''> was not expected.
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderfather.Read4_father()
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
   at Tools.DeSerializeGeneric[T](String source) in Dev-testing:\Tools.cs:line 1549
   at Program.Main(String[] args) in Dev-testing:\Program.cs:line 48

The Tools.DeSerializeGeneric used in code is dummy deserialization func:

    public static T DeSerializeGeneric<T>(string source)
    {
        XmlSerializer x = new XmlSerializer(typeof(T));
        StringReader SsR = new StringReader(source);
        return (T) x.Deserialize(SsR);
    }

Is there a way to force the object to serialize as its base class (as it happens in the list)?

SimSimY
  • 3,616
  • 2
  • 30
  • 35
  • Perhaps [xml-serialize-generic-list-of-serializable-objects](http://stackoverflow.com/questions/1212742/xml-serialize-generic-list-of-serializable-objects) can help you. – Ivan Kochurkin Sep 23 '12 at 22:01
  • @KvanTTT I'm looking for the opposite... When I'm using List, the deserialization works fine. – SimSimY Sep 24 '12 at 00:01

0 Answers0