using System.Collections.Generic;
using System.Xml.Serialization;
[XmlRoot("SyncML", Namespace = "SYNCML:SYNCML1.2")]
public SyncHdr SyncHdr { get; set; }
[XmlArrayItem("Status", Type = typeof(StatusCommand))]
[XmlArrayItem("Get", Type = typeof(GetCommand))]
[XmlArrayItem("Final", Type = typeof(FinalCommand))]
public List<SyncCommandBase> SyncBody { get; set; } = new ();
public string VerDtd { get; set; }
public string VerProto { get; set; }
public abstract class SyncCommandBase
public abstract class SyncCommand : SyncCommandBase
public int CmdId { get; set; }
public class StatusCommand : SyncCommand
public int MsgRef { get; set; }
public int CmdRef { get; set; }
public class GetCommand : SyncCommand
[XmlElement(ElementName="Item")]
public List<Item> Item { get; set; }
public Location Target { get; set; }
public string LocUri { get; set; }
public class FinalCommand : SyncCommandBase
public static void Test()
var root = xml.LoadFromXml<SyncML>();
var xml2 = root.GetXml();
static string GetXml() =>
<SyncML xmlns="SYNCML:SYNCML1.2">
<VerProto>DM/1.2</VerProto>
public static class XmlSerializationHelper
public static T LoadFromXml<T>(this string xmlString, XmlSerializer serial = null)
serial = serial ?? new XmlSerializer(typeof(T));
using (var reader = new StringReader(xmlString))
return (T)serial.Deserialize(reader);
public static string GetXml<T>(this T obj, bool omitStandardNamespaces)
return obj.GetXml(null, omitStandardNamespaces);
public static string GetXml<T>(this T obj, XmlSerializer serializer = null, bool omitStandardNamespaces = false)
XmlSerializerNamespaces ns = null;
if (omitStandardNamespaces)
ns = new XmlSerializerNamespaces();
using (var textWriter = new StringWriter())
var settings = new XmlWriterSettings() { Indent = true };
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
(serializer ?? new XmlSerializer(obj.GetType())).Serialize(xmlWriter, obj, ns);
return textWriter.ToString();
public static void Main()
Console.WriteLine("Environment version: {0} ({1}, {2})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
Console.WriteLine("Failed with unhandled exception: ");