50

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 derived classes which could be assigned to that property.

If I assign any of the derived classes to the container and attempt to serialize the container, the XmlSerializer throws the dreaded:

"The type x was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."

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
{
    [DataMember]
    public PaymentSummary Summary { get; set; }

    public PaymentSummaryRequest()
    {
        Mechanism = CommandMechanism.PaymentSummary;
    }
}

The base class

[DataContract]
[XmlInclude(typeof(xPaymentSummary))]
[XmlInclude(typeof(yPaymentSummary))]
[XmlInclude(typeof(zPaymentSummary))]
[KnownType(typeof(xPaymentSummary))]
[KnownType(typeof(yPaymentSummary))]
[KnownType(typeof(zPaymentSummary))]
public abstract class PaymentSummary
{     
}

One of the derived classes

[DataContract]
public class xPaymentSummary : PaymentSummary
{
}

The serialization code

var serializer = new XmlSerializer(typeof(PaymentSummaryRequest));
serializer.Serialize(Console.Out,new PaymentSummaryRequest{Summary = new xPaymentSummary{}});

The Exception

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type xPaymentSummary 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

3
  • 2
    Why are you mixing DataContract with the XML Serializer? Commented Jan 6, 2011 at 15:37
  • Also, please show the code you use to serialize, and post the full exception with all inner exceptions. Just catch the exception and post ex.ToString(). Commented Jan 6, 2011 at 15:46
  • 1
    They are really seriving 2 different purposes. The class is essentially a DTO used in talking to a WCF service. The service itself is serializing the that DTO and storing it for reporting and troubleshooting purposes. I left the WCF specific attributes in place in the event that there is some spooky action going on between the two.
    – Tedford
    Commented Jan 6, 2011 at 15:46

2 Answers 2

47

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
);
2
  • 1
    I have tried both given that is what the exception detail suggested. Neither attribute resulted in a change in behavior. However, I will update the example approperiately to reduce the confusion.
    – Tedford
    Commented Jan 6, 2011 at 15:43
  • 4
    As a cavet to this it seems the issue exists only with serializing a late-bound element. If the container class was set to use xxxPaymentSummary directly then the XML namespace doesn't need to be synchronized. In any event good find!
    – Tedford
    Commented Jan 6, 2011 at 17:14
1

I had the same issue and some Googling lead me here. I could not accept to have the XMLInclude attribute for each implementation in the base class. I got it to work with a generic serialization method. First off, some of the OP's original example, but slightly modified for clarity:

public abstract class PaymentSummary
{
  public string _summary;
  public string _type;
}

public class xPaymentSummary : PaymentSummary
{
  public xPaymentSummary() { }

  public xPaymentSummary(string summary)
  {
     _summary = summary;
     _type = this.GetType().ToString();
  }
}

In addition to the above, there is also a yPaymentSummary and zPaymentSummary with exactly the same implementation. These are added to a collection, and then the serialization method can be called on each:

List<PaymentSummary> summaries = new List<PaymentSummary>();
summaries.Add(new xPaymentSummary("My summary is X."));
summaries.Add(new yPaymentSummary("My summary is Y."));
summaries.Add(new zPaymentSummary("My summary is Z."));

foreach (PaymentSummary sum in summaries)
  SerializeRecord(sum);

Finally, the serialization method - a simple generic method, that uses the Type of record when serializing:

static void SerializeRecord<T>(T record) where T: PaymentSummary
{
  var serializer = new XmlSerializer(record.GetType());
  serializer.Serialize(Console.Out, record);

  Console.WriteLine(" ");
  Console.WriteLine(" ");
}

The above yields the following output:

enter image description here

1
  • The change from: var serializer = new XmlSerializer(typeof(record)); To: var serializer = new XmlSerializer(record.GetType()); Is the change which removed the error.
    – fishjd
    Commented Jan 21, 2021 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.