1

I have these 3 classes:

Class Image : Asset
Class Sound : Asset
Class Video : Asset

everything serializes ok but when i create this item:

Class Master
List<Asset> assets //property

an instance of this class for example:

Image i = new Image();
Sound s = new Sound();
Video v = new Video();
Master m =  new Master( new List<Asset>{i,s,v} )

it does not serialize with exception "InvalidOperationException-There was an error generating the XML document" and in the innerException : {"The type MyApplication.Video was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."}

.. any idea??

sstauross
  • 2,602
  • 2
  • 30
  • 50
  • 1
    you could help with the exception info – Freeman Mar 01 '13 at 14:21
  • Did you add the Attribute to your class? Does the Image class contains any filereader? Do you marked all nonserializeable member with the NonSerialized Attribute? – Tearsdontfalls Mar 01 '13 at 14:23
  • each one of the Image, Sound, Video instances serializes ok without the Serializeable attribute no the image does not contain filereader.. – sstauross Mar 01 '13 at 14:26

1 Answers1

6

Add the XmlInclude attribute on the Asset class:

[XmlInclude(typeof(Video))]
[XmlInclude(typeof(Sound))]
[XmlInclude(typeof(Image))]
public class Asset
{
    ...
}
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758