title: Is there a reason why a base class decorated with XmlInclude would still throw a type unknown exception when serialized?
body: <p>I will simplify the code to save space but what is presented does illustrate the core problem.</p>
  
  <p>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. </p>
  
  <p>If I assign any of the derived classes to the container and attempt to serialize the container, the XmlSerializer throws the dreaded: </p>
  
  <blockquote>
    <p>"The type x was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically." </p>
  </blockquote>
  
  <p>However my base class is already decorated with that attribute so I figure there must be an additional "hidden" requirement.</p>
  
  <p>The really odd part is that the default WCF serializer has no issues with this class hierarchy.</p>
  
  <p><strong>The Container class</strong></p>
  
  <pre><code>[DataContract]
  [XmlRoot(ElementName = "TRANSACTION", Namespace = Constants.Namespace)]
  public class PaymentSummaryRequest : CommandRequest
  {
      [DataMember]
      public PaymentSummary Summary { get; set; }
  
      public PaymentSummaryRequest()
      {
          Mechanism = CommandMechanism.PaymentSummary;
      }
  }
  </code></pre>
  
  <p><strong>The base class</strong> </p>
  
  <pre><code>[DataContract]
  [XmlInclude(typeof(xPaymentSummary))]
  [XmlInclude(typeof(yPaymentSummary))]
  [XmlInclude(typeof(zPaymentSummary))]
  [KnownType(typeof(xPaymentSummary))]
  [KnownType(typeof(yPaymentSummary))]
  [KnownType(typeof(zPaymentSummary))]
  public abstract class PaymentSummary
  {     
  }
  </code></pre>
  
  <p><strong>One of the derived classes</strong></p>
  
  <pre><code>[DataContract]
  public class xPaymentSummary : PaymentSummary
  {
  }
  </code></pre>
  
  <p><strong>The serialization code</strong></p>
  
  <pre><code>var serializer = new XmlSerializer(typeof(PaymentSummaryRequest));
  serializer.Serialize(Console.Out,new PaymentSummaryRequest{Summary = new xPaymentSummary{}});
  </code></pre>
  
  <p><strong>The Exception</strong></p>
  
  <blockquote>
    <p>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</p>
    
    <blockquote>
      <p>Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterPaymentSummaryRequest.Write13_PaymentSummary(String
      n, String ns, PaymentSummary o,
      Boolean isNullable, Boolean needType) 
      at</p>
      
      <blockquote>
        <p>Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterPaymentSummaryRequest.Write14_PaymentSummaryRequest(String
        n, String ns, PaymentSummaryRequest o,
        Boolean isNullable, Boolean needType) 
        at</p>
        
        <blockquote>
          <p>Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterPaymentSummaryRequest.Write15_TRANSACTION(Object
          o)    --- End of inner exception stack
          trace ---    at</p>
          
          <blockquote>
            <p>System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter
            xmlWriter, Object o,
            XmlSerializerNamespaces namespaces,
            String encodingStyle, String id)    at</p>
            
            <blockquote>
              <p>System.Xml.Serialization.XmlSerializer.Serialize(TextWriter
              textWriter, Object o,
              XmlSerializerNamespaces namespaces)<br>
              at UserQuery.RunUserAuthoredQuery() in
              c:\Users\Tedford\AppData\Local\Temp\uqacncyo.0.cs:line
              47</p>
            </blockquote>
          </blockquote>
        </blockquote>
      </blockquote>
    </blockquote>
  </blockquote>