Is there a reason why a base class decorated with XmlInclude would still throw a type unknown exception when serialized?

后端 未结 2 1124
囚心锁ツ
囚心锁ツ 2020-12-08 13:28

I will simplify the code to save space but what is presented does illustrate the core problem.

I have a class which has a property that is a base type. There are 3 d

2条回答
  •  粉色の甜心
    2020-12-08 13:58

    The issue you are seeing is because the PaymentSummaryRequest is setting the Namespace. You can either remove the Namespace or add a Namespace to the PaymentSummary class:

    [XmlRoot(Namespace = Constants.Namespace)]
    [XmlInclude(typeof(xxxPaymentSummary))]
    public abstract class PaymentSummary
    {
    }
    

    As @Tedford mentions in his comment below the namespace is only required when using derived types.

    It looks like when generating the XML Serialization assembly that since the Root node has a namespace set and the base class does not it is not including the XML Include logic in the generated serialization assembly.

    Another approach to solving the issue would be to remove the namespace declarations from the classes themselves and specify the namespace on the XmlSerializer constructor:

    var serializer = new XmlSerializer(
        typeof(PaymentSummaryRequest), 
        Constants.Namespace
    );
    

提交回复
热议问题