Serialize classes keeping hierarchy tree

MegaSave

New Member
I want to serialize class hierarchy and keep hierarchy tree in resulting xml. I set Message property of ProtokolMessage classwith Heartbeat object which implements Message abstract class. As the output result I want to get following xml:\[code\]<protocol> <name>someName</name> <messageId>1101</messageId> <heartbeat> <time>2013-04-02T17:35:55</time> </heartbeat></protocol>\[/code\]However resulting xml is:\[code\]<protocol> <Message xsi:type="heartbeat" /> <name>someName</name> <messageID xmlns="Message">1101</messageID></protocol>\[/code\]Domain model:\[code\][XmlRoot("protocol")]public class ProtocolMessage{ [XmlElement(ElementName = "name")] public string Name { get; set; } [XmlElement(ElementName = "messageID")] public string MessageID { get; set; } public Message Message {get; set;} public ProtocolMessage() {}}[XmlInclude(typeof(Heartbeat))]public abstract class Message{ public Message() { }}[XmlType(TypeName = "heartbeat")]public class Heartbeat : Message{ [XmlElement("time")] protected string Time { get; set; } public Heartbeat() : this(DateTime.Now) { } public Heartbeat(DateTime dateTime) { Time = dateTime.ToString("s"); }}public class Program{ static void Main(string[] args) { var protocolMsg = new ProtocolMessage { Name = "someName", MessageId = "1101", Message = new Heartbeat(); }; var serializer = new XmlSerializer(typeof(ProtocolMessage)); StringWriter sw = new StringWriter(); serializer.Serialize(sw, this); }}\[/code\]Can I get hierarchy tree in xml?
 
Back
Top