Muốn serialise dữ liệu của tôi vào đây:Làm thế nào để XML serialize mảng đa hình mà không cần gói yếu tố
<?xml version="1.0" encoding="ibm850"?>
<Batch Name="Test batch">
<ExecuteCommand Command="..." />
<WaitCommand Seconds="5" />
</Batch>
Nhưng thay vào đó tôi nhận được này (lưu ý các gói lệnh yếu tố)
<?xml version="1.0" encoding="ibm850"?>
<Batch Name="Test batch">
<Commands><!-- I want to get rid of thiw wrapper Commands element and just -->
<ExecuteCommand Command="..." />
<WaitCommand Seconds="5" />
</Commands>
</Batch>
Đây là mã mẫu được sử dụng để tạo mẫu này:
public class BaseCommand //base class
{
[XmlAttribute]
public string Result { get; set; }
}
public class ExecuteCommand : BaseCommand
{
[XmlAttribute]
public string Command { get; set; }
}
public class WaitCommand : BaseCommand
{
[XmlAttribute]
public int Seconds { get; set; }
}
public class Batch
{
[XmlAttribute]
public string Name { get; set; }
private List<BaseCommand> _commands = new List<BaseCommand>();
[XmlArrayItem(typeof(ExecuteCommand))]
[XmlArrayItem(typeof(WaitCommand))]
public List<BaseCommand> Commands
{
get
{
return _commands;
}
set
{
_commands = value;
}
}
public static void Main()
{
XmlSerializer serializer = new XmlSerializer(typeof(Batch));
Batch b = new Batch();
b.Name = "Test batch";
b.Commands.Add(new ExecuteCommand() { Command = "..." });
b.Commands.Add(new WaitCommand() { Seconds = 5 });
serializer.Serialize(Console.Out, b);
Console.Read();
}
}
Tôi đã tìm và đọc đống bài viết về chủ đề này. Tất cả chúng dường như cung cấp giải pháp cho việc sắp xếp các bộ sưu tập với một loại lớp duy nhất (không sử dụng thừa kế). Tôi sử dụng thừa kế và không có gì có vẻ hiệu quả. Rất tiếc, tôi phải xuất tài liệu XML chính xác do hỗ trợ cũ
Tốt tìm ...... – GONeale