using System.Collections.Generic;
using System.Xml.Serialization;
namespace CustomerXmlSerializer
public static void Main(string[] args)
private static void CustomXmlExample1()
Console.WriteLine("Example 1");
Console.WriteLine("Example of a custom root element with any object:");
XmlSerializer xsSubmit = new XmlSerializer(typeof(CommonXmlRoot), new XmlRootAttribute("MyCustomRootName"));
var commonXmlRoot = new CommonXmlRoot { Account = new Account { BillingAccountNumber = "1122112" } };
var ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, string.Empty);
using (var sww = new StringWriter())
using XmlWriter writers = XmlWriter.Create(sww);
xsSubmit.Serialize(writers, commonXmlRoot, ns);
Console.WriteLine($"{sww}\n");
public class CommonXmlRoot
public Account Account { get; set; }
public string BillingAccountNumber { get; set; }
private static void CustomXmlExample2()
Console.WriteLine("Example 2");
Console.WriteLine("Example of a custom root element with custom first level element name:");
var ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, string.Empty);
XmlSerializer xsSubmit = new XmlSerializer(typeof(CustomCommonXmlRoot), new XmlRootAttribute("MyCustomRootName"));
var customCommonXmlRoot = new CustomCommonXmlRoot("MyCustomFirstLevelAttributeName") { BillingAccountNumber = "1122112" };
using (var sww = new StringWriter())
using XmlWriter writers = XmlWriter.Create(sww);
xsSubmit.Serialize(writers, customCommonXmlRoot, ns);
Console.WriteLine($"{sww}\n");
public class CustomCommonXmlRoot : IXmlSerializable
private readonly string _firstLevelAttributeName;
public string BillingAccountNumber { get; set; }
public CustomCommonXmlRoot(string firstLevelAttributeName)
_firstLevelAttributeName = firstLevelAttributeName;
public CustomCommonXmlRoot()
public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
throw new NotImplementedException();
public void WriteXml(XmlWriter writer)
writer.WriteStartElement(_firstLevelAttributeName);
writer.WriteElementString(nameof(BillingAccountNumber), BillingAccountNumber);
writer.WriteEndElement();
private static void CustomXmlExample3()
Console.WriteLine("Example 3");
Console.WriteLine("Example of a custom root element with custom first level element name and custom data:");
var ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, string.Empty);
XmlSerializer xsSubmit = new XmlSerializer(typeof(CustomDynamicCommonXmlRoot), new XmlRootAttribute("MyCustomRootName"));
var customDynamicCommonXmlRoot = new CustomDynamicCommonXmlRoot("MyCustomFirstLevelAttributeName");
customDynamicCommonXmlRoot.Data.Add("BillingAccountNumber", "1122112");
using (var sww = new StringWriter())
using XmlWriter writers = XmlWriter.Create(sww);
xsSubmit.Serialize(writers, customDynamicCommonXmlRoot, ns);
Console.WriteLine($"{sww}\n");
public class CustomDynamicCommonXmlRoot : IXmlSerializable
private readonly string _firstLevelAttributeName;
public Dictionary<string, string> Data { get; } = new Dictionary<string, string>();
public CustomDynamicCommonXmlRoot(string firstLevelAttributeName)
_firstLevelAttributeName = firstLevelAttributeName;
public CustomDynamicCommonXmlRoot()
public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
throw new NotImplementedException();
public void WriteXml(XmlWriter writer)
writer.WriteStartElement(_firstLevelAttributeName);
foreach (var item in Data)
writer.WriteElementString(item.Key, item.Value);
writer.WriteEndElement();
private static void CustomXmlExample4()
Console.WriteLine("Example 4");
Console.WriteLine("Example of a custom root element with custom first level element name and custom dynamic data:");
var ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, string.Empty);
XmlSerializer xsSubmit = new XmlSerializer(typeof(CustomDynamicDataCommonXmlRoot), new XmlRootAttribute("MyCustomRootName"));
var customDynamicCommonXmlRoot = new CustomDynamicDataCommonXmlRoot("MyCustomFirstLevelAttributeName");
customDynamicCommonXmlRoot.Data = new { BillingAccountNumber = "1212112", MoreInfo = new { SomeData = 121m, DeepInfo = new { MoreData = DateTime.Now } } };
using (var sww = new StringWriter())
using XmlWriter writers = XmlWriter.Create(sww);
xsSubmit.Serialize(writers, customDynamicCommonXmlRoot, ns);
Console.WriteLine($"{sww}\n");
public class CustomDynamicDataCommonXmlRoot : IXmlSerializable
private readonly string _firstLevelAttributeName;
public object Data { get; set; }
public CustomDynamicDataCommonXmlRoot(string firstLevelAttributeName)
_firstLevelAttributeName = firstLevelAttributeName;
public CustomDynamicDataCommonXmlRoot()
public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
throw new NotImplementedException();
public void WriteXml(XmlWriter writer)
WriteXml(writer, Data.GetType(), _firstLevelAttributeName, Data);
private void WriteXml(XmlWriter writer, Type t, string rootName, object data)
writer.WriteStartElement(rootName);
foreach (var propInfo in t.GetProperties())
if (propInfo.PropertyType != typeof(string) && propInfo.PropertyType.IsClass)
WriteXml(writer, propInfo.PropertyType, propInfo.Name, propInfo.GetValue(data));
writer.WriteElementString(propInfo.Name, propInfo.GetValue(data).ToString());
writer.WriteEndElement();