2

I have a serializable class which contains some properties, including a variable of type object. I want my class to be able to contain different (of course serializable) objects. The problem is, that I get an exception during serialization:

Use the XmlInclude or SoapInclude attribute to specify types that are not known statically

I created a small sample for this issue:

class Program
{
    static void Main(string[] args)
    {
        SerializeableTestClass testClass = new SerializeableTestClass();

        testClass.Params = new ExtendedParams();

        MemoryStream ms = new MemoryStream();
        XmlSerializer xmlf = new XmlSerializer(testClass.GetType());
        xmlf.Serialize(ms, testClass);
        ms.Capacity = (int)ms.Length;
        ms.Close();
        byte[] array =  ms.GetBuffer();
    }
}

[Serializable]
public class SerializeableTestClass
{
    [XmlElement(ElementName = "Params")]
    public object Params;
}

[Serializable]
public class ParamsBase
{
    [XmlElement(ElementName = "SomeValue")]
    public Int32 SomeValue;
}

[Serializable]
public class ExtendedParams : ParamsBase
{
    [XmlElement(ElementName = "SomeNewValue")]
    public Int32 SomeNewValue;
}

Is there a possibility to serialize and deserialize this class without specifying the concrete type of "Params" ???

Best regards

leppie
  • 115,091
  • 17
  • 196
  • 297
Emu
  • 87
  • 3
  • possible duplicate of [How to serialize property of type Object with XmlSerializer](http://stackoverflow.com/questions/9497310/how-to-serialize-property-of-type-object-with-xmlserializer) – Nikita B Apr 24 '14 at 07:04

3 Answers3

2

Is it acceptable to include all the possible types on the class that is serialized? (That is what the message means, that no type info is included for ExtendedParams).

You can include it in the XmlSerializeritself, or include it on the main class:

[Serializable]
[XmlInclude(typeof(ParamsBase))]
[XmlInclude(typeof(ExtendedParams))]
public class SerializeableTestClass
{
    [XmlElement(ElementName = "Params")]    
    public object Params;
}

But to dynamically serialize if you don't know all the types:

//static: XmlSerializer xmlf = new XmlSerializer(testClass.GetType(),new Type[]{typeof(ExtendedParams)});

//dynamic:
Type[] extratypes = testClass.Params == null ? null : new Type[] { testClass.Params.GetType() };
XmlSerializer xmlf = new XmlSerializer(testClass.GetType(), extratypes );
Me.Name
  • 12,259
  • 3
  • 31
  • 48
  • @Emu Added dynamic serialialization option, for the case you describe that the parameter type is not known beforehand – Me.Name Apr 24 '14 at 07:20
1

modify your code , when you initialize the XmlSerializer

 XmlSerializer xmlf = new XmlSerializer(testClass.GetType(),new Type [] {typeof(ExtendedParams)});

this will let the XmlSerializer know about the other types in the class

Mohammad Gabr
  • 221
  • 2
  • 5
  • 1
    Yes this solution works. So there is no way to serialize the SerializeableTestClass without specifying the type of the Params object? – Emu Apr 24 '14 at 07:21
  • I think you can use this line for more generalization XmlSerializer xmlf = new XmlSerializer(testClass.GetType(),new Type [] {testClass.Params.GetType()}); – Mohammad Gabr Apr 24 '14 at 07:33
0

As the exception message suggests, you need to specify the derived type so that it can be recognized:

[Serializable]
[XmlInclude(typeof(ExtendedParams))]
public class SerializeableTestClass
{
    [XmlElement(ElementName = "Params")]
    public object Params;
}
Fung
  • 3,508
  • 2
  • 26
  • 33
  • 1
    Hmm, I don't want the SerializeableTestClass to know about the type of the Params, because that means that I have to modify the source of my SerializeableTestClass every time a new object type will be added – Emu Apr 24 '14 at 07:12