using System; using System.Xml; using System.Diagnostics; using System.Text.Json; using System.Xml.Linq; using System.Xml.Serialization; public class Message { public Message(int from, int to, string msg) { Debug.Assert( !string.IsNullOrEmpty( msg ) ); this.From = from; this.To = to; this.Msg = msg; } public int From { get; } public int To { get; } public string Msg { get; } public override string ToString() { return $"From: {From} -> To: {To}: \"{Msg}\""; } } // ## JSON public class JsonMessage { public JsonMessage(Message m) { this.Message = m; } public void Save(string fn) { File.WriteAllText( fn, JsonSerializer.Serialize( this.Message ) ); } public static Message Load(string fn) { return JsonSerializer.Deserialize( File.ReadAllText( fn ) ?? throw new IOException( "error reading file: " + fn ) ) ?? throw new JsonException( "error reading JSON from: " + fn ); } public Message Message { get; } } // ## XML 1 (doesn't work, no parameterless constructor in Message) public class XmlMessage { public XmlMessage(Message m) { this.Message = m; } public Message Message { get; } public void Save(string fn) { using ( var f = File.OpenWrite( fn ) ) { new XmlSerializer( this.Message.GetType() ) .Serialize( f, this.Message ); } } public static Message Load(string fn) { using ( var f = File.OpenRead( fn ) ) { return (Message) new XmlSerializer( typeof( Message ) ) .Deserialize( f ) ?? throw new XmlException( "error reading XML from: " + fn ); } } } // ## XML 2 (bespoken XML) public class XmlMessage2 { public XmlMessage2(Message m) { this.Message = m; } public Message Message { get; } public void Save(string fn) { new XElement("Message", new XAttribute( "from", this.Message.From ), new XAttribute( "to", this.Message.To ), this.Message.Msg ).Save( fn ); } public static Message Load(string fn) { var msgElem = XElement.Load( fn ) ?? throw new IOException( "reading " + fn ); int from = (int?) msgElem.Attribute( "from" ) ?? throw new XmlException( "missing from" ); int to = (int?) msgElem.Attribute( "to" ) ?? throw new XmlException( "missing to" ); return new Message( from, to, msgElem.Value ); } } // ## XML 3 (using a DTO class) public class MessageDTO { public int From { get; set; } public int To { get; set; } public string Msg { get; set; } public override string ToString() { return $"[MessageDTO [From: {From}][To: {To}]: \"{Msg}\"]"; } public Message ToMessage() { return new Message( this.From, this.To, this.Msg ); } public static MessageDTO FromMessage(Message m) { return new MessageDTO { From = m.From, To = m.To, Msg = m.Msg }; } } public class XmlMessage3 { public XmlMessage3(Message m) { this.Message = m; } public Message Message { get; } public void Save(string fn) { using ( var f = File.OpenWrite( fn ) ) { new XmlSerializer( typeof( MessageDTO ) ) .Serialize( f, MessageDTO.FromMessage( this.Message ) ); } } public static Message Load(string fn) { using ( var f = File.OpenRead( fn ) ) { return ( (MessageDTO) new XmlSerializer( typeof( MessageDTO ) ) .Deserialize( f ) ).ToMessage() ?? throw new XmlException( "error reading XML from: " + fn ); } } } public class TestSerialization { public static void Main() { var m1 = new Message( 455123132, 555789789, "¿Peli y manta esta noche?" ); new JsonMessage( m1 ).Save( "msg.json" ); Console.WriteLine( JsonMessage.Load( "msg.json" ) ); new XmlMessage2( m1 ).Save( "msg.xml" ); Console.WriteLine( XmlMessage2.Load( "msg.xml" ) ); new XmlMessage3( m1 ).Save( "msg.xml" ); Console.WriteLine( XmlMessage3.Load( "msg.xml" ) ); } }