title: XML Serializing information on Base class and inherited class
body: <p>I am trying to serialize a C# object into XML so that it could be used as the body of API call.  They are very particular about the input they need. I have built the following class to hold the data I need to send over to them. Including attributes and all properties as Elements instead of attributes. They also require that lists include the <code>type="array"</code> I though that creating my own class the implements a List would be the easiest since all lists I give them must have the same attribute.  When serialization occurs it serializes the base class of List items but it doesn't include the attribute I want from the derived class. </p>
  
  <pre class="lang-cs prettyprint-override"><code>public class CustomArray&lt;T&gt; : List&lt;T&gt;
  {
      [XmlAttribute]
      public string type { get; set; } = "array";
  }
  
  [XmlRoot("message")]
  public class MessageBody
  {
      [XmlArray("Checks"), XmlArrayItem("CheckItem")]
      public CustomArray&lt;Check&gt; CheckList { get; set; }
  }
  
  public class Check
  {
      [XmlElement("C_CHECK_NUMBER")]
      public string CheckNumber { get; set; }
  
      [XmlElement("C_CHECK_AMOUNT")]
      public decimal Amount { get; set; }
  
      [XmlArray("InvoiceList"), XmlArrayItem("Invoice")]
      public CustomArray&lt;Invoice&gt; InvoiceList { get; set; }
  }
  
  public class Invoice
  {
      [XmlElement("C_INVOICE_ID")]
      public long ID { get; set; }
  
      [XmlElement("C_INVOICE_NUM")]
      public string InvoiceNum { get; set; }
  }
  </code></pre>
  
  <p>I then run this code:</p>
  
  <pre class="lang-cs prettyprint-override"><code>// Create a sample object
  var message = new MessageBody()
  {
      CheckList = new CustomArray&lt;Check&gt;
      {
          new Check
          {
              CheckNumber = "111",
              Amount = 1.00M
          },
          new Check
          {
              CheckNumber = "112",
              Amount = 2.00M,
              InvoiceList = new CustomArray&lt;Invoice&gt;
              {
                  new Invoice
                  {
                      ID = 1,
                      InvoiceNum = "1"
                  }
              }
          }
      }
  };
  
  // Create custom settings
  var settings = new XmlWriterSettings
  {
      OmitXmlDeclaration = true,
      Indent = true
  };
  
  // Serialize item and print it to console
  using (var sw = new StringWriter())
  using (var writer = XmlWriter.Create(sw, settings))
  {
      var serializer = new XmlSerializer(message.GetType());
      serializer.Serialize(writer, message, new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }));
      Console.WriteLine(sw.ToString());
  }
  </code></pre>
  
  <p>I get this written to the console:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;message&gt;
    &lt;Checks&gt;
      &lt;CheckItem&gt;
        &lt;C_CHECK_NUMBER&gt;111&lt;/C_CHECK_NUMBER&gt;
        &lt;C_CHECK_AMOUNT&gt;1.00&lt;/C_CHECK_AMOUNT&gt;
      &lt;/CheckItem&gt;
      &lt;CheckItem&gt;
        &lt;C_CHECK_NUMBER&gt;112&lt;/C_CHECK_NUMBER&gt;
        &lt;C_CHECK_AMOUNT&gt;2.00&lt;/C_CHECK_AMOUNT&gt;
        &lt;InvoiceList&gt;
            &lt;Invoice&gt;
                &lt;C_INVOICE_ID&gt;1&lt;/C_INVOICE_ID&gt;
                &lt;C_INVOICE_NUM&gt;1&lt;/C_INVOICE_NUM&gt;
            &lt;/Invoice&gt;
        &lt;/InvoiceList&gt;
      &lt;/CheckItem&gt;
    &lt;/Checks&gt;
  &lt;/message&gt;
  </code></pre>
  
  <p>But I need to get this:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;message&gt;
    &lt;Checks type="array"&gt;
      &lt;CheckItem&gt;
        &lt;C_CHECK_NUMBER&gt;111&lt;/C_CHECK_NUMBER&gt;
        &lt;C_CHECK_AMOUNT&gt;1.00&lt;/C_CHECK_AMOUNT&gt;
      &lt;/CheckItem&gt;
      &lt;CheckItem&gt;
        &lt;C_CHECK_NUMBER&gt;112&lt;/C_CHECK_NUMBER&gt;
        &lt;C_CHECK_AMOUNT&gt;2.00&lt;/C_CHECK_AMOUNT&gt;
        &lt;InvoiceList type="array"&gt;
            &lt;Invoice&gt;
                &lt;C_INVOICE_ID&gt;1&lt;/C_INVOICE_ID&gt;
                &lt;C_INVOICE_NUM&gt;1&lt;/C_INVOICE_NUM&gt;
            &lt;/Invoice&gt;
        &lt;/InvoiceList&gt;
      &lt;/CheckItem&gt;
    &lt;/Checks&gt;
  &lt;/message&gt;
  </code></pre>
  
  <p>Thank you for your help!</p>
  
  <p>Here is a dotnetfiddle that I made to show it off. It's not exact but it has the same idea. <a href="https://dotnetfiddle.net/ALCX5H" rel="nofollow noreferrer">https://dotnetfiddle.net/ALCX5H</a></p>