Is there a reason why a base class decorated with XmlInclude would still throw a type unknown exception when serialized?
- by Tedford
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 exist 3 dervived classes which could be assigned to that property. If I assign any of the derived classes to the container then the XmlSerializer throws dreaded "The type xxx was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically." exception when attempting to seralize the container. However my base class is already decorated with that attribute so I figure there must be an additional "hidden" requirement. The really odd part is that the default WCF serializer has no issues with this class hierarchy.
The Container class
[DataContract]
[XmlRoot(ElementName = "TRANSACTION", Namespace = Constants.Namespace)]
public class PaymentSummaryRequest : CommandRequest
{
/// <summary>
/// Gets or sets the summary.
/// </summary>
/// <value>The summary.</value>
/// <remarks></remarks>
[DataMember]
public PaymentSummary Summary { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="PaymentSummaryRequest"/> class.
/// </summary>
public PaymentSummaryRequest()
{
Mechanism = CommandMechanism.PaymentSummary;
}
}
The base class
[DataContract]
[XmlInclude(typeof(xxxPaymentSummary))]
[XmlInclude(typeof(yyyPaymentSummary))]
[XmlInclude(typeof(zzzPaymentSummary))]
[KnownType(typeof(xxxPaymentSummary))]
[KnownType(typeof(xxxPaymentSummary))]
[KnownType(typeof(zzzPaymentSummary))]
public abstract class PaymentSummary
{
}
One of the derived classes
[DataContract]
public class xxxPaymentSummary : PaymentSummary
{
}
The serialization code
var serializer = new XmlSerializer(typeof(PaymentSummaryRequest));
serializer.Serialize(Console.Out,new PaymentSummaryRequest{Summary = new xxxPaymentSummary{}});
The Exception
System.InvalidOperationException:
There was an error generating the XML
document. ---
System.InvalidOperationException: The
type xxxPaymentSummary was not
expected. Use the XmlInclude or
SoapInclude attribute to specify types
that are not known statically. at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterPaymentSummaryRequest.Write13_PaymentSummary(String
n, String ns, PaymentSummary o,
Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterPaymentSummaryRequest.Write14_PaymentSummaryRequest(String
n, String ns, PaymentSummaryRequest o,
Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterPaymentSummaryRequest.Write15_TRANSACTION(Object
o) --- End of inner exception stack
trace --- at
System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter
xmlWriter, Object o,
XmlSerializerNamespaces namespaces,
String encodingStyle, String id) at
System.Xml.Serialization.XmlSerializer.Serialize(TextWriter
textWriter, Object o,
XmlSerializerNamespaces namespaces)
at UserQuery.RunUserAuthoredQuery() in
c:\Users\Tedford\AppData\Local\Temp\uqacncyo.0.cs:line
47