Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've got a very strange problem when working with .NET's XmlSerializer.

Take the following example classes:

public class Order 
{
    public PaymentCollection Payments { get; set; }

    //everything else is serializable (including other collections of non-abstract types)
}

public class PaymentCollection : Collection<Payment>
{
}

public abstract class Payment 
{
    //abstract methods
}

public class BankPayment : Payment
{
    //method implementations
}

AFAIK, there are three different methods to solve the InvalidOperationException that's caused by the serializer not knowing about the derived types of Payment.

1. Adding XmlInclude to the Payment class definition:

This is not possible due to all classes being included as external references over which I have no control of.

2. Passing the derived types' types during creation of the XmlSerializer instance

Doesn't work.

3. Defining XmlAttributeOverrides for the target property in order to override the property's default serialization (as explained in this SO post)

Also doesn't work (XmlAttributeOverrides initialization follows).

Type bankPayment = typeof(BankPayment);

XmlAttributes attributes = new XmlAttributes();
attributes.XmlElements.Add(new XmlElementAttribute(bankPayment.Name, bankPayment));

XmlAttributeOverrides overrides = new XmlAttributeOverrides();
overrides.Add(typeof(Order), "Payments", attributes);

The appropriate XmlSerializer constructor would then be used.

NOTE: by doesn't work I mean the InvalidOperationException (BankPayment was not expected...) is thrown.

Can anyone shed some light on the subject? How would one go about and debug the issue further?

like image 757
lsoliveira Avatar asked Aug 09 '12 15:08

lsoliveira


3 Answers

This worked for me:

[XmlInclude(typeof(BankPayment))]
[Serializable]
public abstract class Payment { }    

[Serializable]
public class BankPayment : Payment {} 

[Serializable]
public class Payments : List<Payment>{}

XmlSerializer serializer = new XmlSerializer(typeof(Payments), new Type[]{typeof(Payment)});
like image 174
bizl Avatar answered Nov 16 '22 08:11

bizl


Just solved the issue. After digging around for a while longer, I found this SO post which covers the exact same situation. It got me in the right track.

Basically, the XmlSerializer needs to know the default namespace if derived classes are included as extra types. The exact reason why this has to happen is still unknown but, still, serialization is working now.

like image 44
lsoliveira Avatar answered Nov 16 '22 07:11

lsoliveira


Base on this I was able to solve this by changing the constructor of XmlSerializer I was using instead of changing the classes.

Instead of using something like this (suggested in the other answers):

[XmlInclude(typeof(Derived))]
public class Base {}

public class Derived : Base {}

public void Serialize()
{
    TextWriter writer = new StreamWriter(SchedulePath);
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Derived>));
    xmlSerializer.Serialize(writer, data);
    writer.Close();
}

I did this:

public class Base {}

public class Derived : Base {}

public void Serialize()
{
    TextWriter writer = new StreamWriter(SchedulePath);
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Derived>), new[] { typeof(Derived) });
    xmlSerializer.Serialize(writer, data);
    writer.Close();
}
like image 5
derekantrican Avatar answered Nov 16 '22 07:11

derekantrican