title: How do I serialize multple objects with the same base class to xml?
body: <p>Here's the code I'm using. I keep getting xml document errors</p>
  
  <pre><code>[Serializable]
  [XmlRoot("Command")]
  public class Command
  {
      [XmlElement("CommandType")]
      public CommandType CommandType { get; set; }
  }
  
  [Serializable]
  [XmlRoot("DelayCommand")]
  [XmlInclude(typeof(Command))]
  public class DelayCommand : Command
  {
      [XmlElement("Delay")]
      public int Delay { get; set; }
  
      public DelayCommand()
      {
          CommandType = CommandType.Delay;
      }
  }
  
  [Serializable]
  [XmlRoot("HeartbeatCommand")]
  [XmlInclude(typeof(Command))]
  public class HeartbeatCommand : Command
  {
      [XmlElement("HeartbeatOn")]
      public bool HeartbeatOn { get; set; }
  
      public HeartbeatCommand()
      {
          CommandType = CommandType.Heartbeat;
      }
  }
  </code></pre>
  
  <p>And here's the code to actually serialize</p>
  
  <pre><code>    FileStream Filewriter = new FileStream(path, FileMode.OpenOrCreate);
  
      XmlSerializer XmlFormat = new XmlSerializer(typeof(Command[])); // Make class as an array.
  
      List&lt;Command&gt; commands = new List&lt;Command&gt;();
      foreach (DataGridViewRow row in gridCommand.Rows)
      {
           commands.Add(row.Tag as Command);
      }
  
      XmlFormat.Serialize(Filewriter, commands.ToArray());
  </code></pre>