title: C# XML Serializing with Abstract Class
body: <p>I'm currently trying to set up some commands as classes for serializing for a communication protocol.  My code is basically as follows:</p>
  
  <pre><code>[XmlRoot("Message")]
  [Serializable]
  public class Message
  {
      private Command[] _commands;
  
      [XmlAttribute("ver")]
      public int Version { get; set; }
      [XmlAttribute("msid")]
      public Guid Id { get; set; }
  
      [XmlArray("Commands")]
      [XmlArrayItem(typeof(HealthCheckCommand))]
      [XmlArrayItem(typeof(TestCommand))]
      public Command[] Commands
      {
          get { return _commands; }
          set { _commands = value; }
      }
  }
  
  public enum CommandTypes
  {
      healthcheck
  }
  
  [XmlType(TypeName = "Command")]
  public abstract class Command
  {
      String CommandType { get; set; }
  }
  
  public class HealthCheckCommand : Command
  {
      [XmlAttribute("type")]
      public string CommandType
      {
          get { return "healthcheck"; }
          set { throw new NotImplementedException(); }
      }
  }
  
  public class TestCommand : Command
  {
      [XmlAttribute("type")]
      public string CommandType
      {
          get { return "test"; }
          set { throw new NotImplementedException(); }
      }
  }
  </code></pre>
  
  <p>What I need to get from this is:</p>
  
  <pre><code>&lt;Message ver="1" msid="00000000-0000-0000-0000-000000000000"&gt;
    &lt;Commands&gt;
      &lt;Command type="healthcheck" /&gt;
      &lt;Command type="test" /&gt;
    &lt;/Commands&gt;
  &lt;/Message&gt;
  </code></pre>
  
  <p>What I'm getting is:</p>
  
  <pre><code>&lt;Message ver="1" msid="00000000-0000-0000-0000-000000000000"&gt;
    &lt;Commands&gt;
      &lt;HealthCheckCommand type="healthcheck" /&gt;
      &lt;TestCommand type="test" /&gt;
    &lt;/Commands&gt;
  &lt;/CTM&gt;
  </code></pre>
  
  <p>When I try to override the XmlArrayItem name with the same name, it of course throws an error.  If I use a list, then it works, but I get all the namespace stuff inside of the subtypes, which I don't want.  I could go in and remove those namespace items after the fact, but I'd rather not.  There must be a way to do this.  </p>
  
  <p>Thanks for the help!</p>
  
  <p>EDIT:</p>
  
  <p>Here's the serialization code:</p>
  
  <pre><code>XmlSerializer serializer = new XmlSerializer(typeof (Message));
          using (TextWriter writer = new StreamWriter(@"C:\Xml.txt"))
          {
              XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces();
              xmlSerializerNamespaces.Add("", "");
              XmlWriterSettings settings = new XmlWriterSettings();
              settings.OmitXmlDeclaration = true;
  
              using (
              XmlWriter xmlwriter = XmlWriter.Create(writer, new XmlWriterSettings { OmitXmlDeclaration = true }))
              {
                  serializer.Serialize(xmlwriter, message, xmlSerializerNamespaces);
              }
          }
      }
  </code></pre>