2010-08-04 14 views
0

Voici le code que j'utilise. Je continue à obtenir des erreurs de document xmlComment sérialiser plusieurs objets avec la même classe de base en xml?

[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; 
    } 
} 

Et voici le code de sérialisation en fait

FileStream Filewriter = new FileStream(path, FileMode.OpenOrCreate); 

    XmlSerializer XmlFormat = new XmlSerializer(typeof(Command[])); // Make class as an array. 

    List<Command> commands = new List<Command>(); 
    foreach (DataGridViewRow row in gridCommand.Rows) 
    { 
     commands.Add(row.Tag as Command); 
    } 

    XmlFormat.Serialize(Filewriter, commands.ToArray()); 

Répondre

0

Il ressemble this pourrait être votre réponse.

+0

Cet exemple ne montre pas plusieurs classes héritées différentes sérialisées. Il n'a que 1 classe et sérialise un tableau de cette classe – Tom