using System.Collections.Generic;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
public abstract class StreamingXmlSerializableBase : IXmlSerializable
protected abstract void Populate(XmlReader reader);
public XmlSchema GetSchema() => null;
public void ReadXml(XmlReader reader)
using (var subReader = reader.ReadSubtree())
subReader.MoveToContent();
public abstract void WriteXml(XmlWriter writer);
public abstract class XmlSerializableBase : IXmlSerializable
protected abstract void Populate(XElement element);
public XmlSchema GetSchema() => null;
public void ReadXml(XmlReader reader)
var element = (XElement)XNode.ReadFrom(reader);
public abstract void WriteXml(XmlWriter writer);
public class SerializableClass : XmlSerializableBase
public string Title { get; set; } = "Test title";
public string Description { get; set; } = "Super description";
public int Number { get; set; } = (int)(DateTime.Now.Ticks % 99);
protected override void Populate(XElement element)
this.Title = (string)element.Element(nameof(this.Title));
this.Description = (string)element.Element(nameof(this.Description));
this.Number = (int?)element.Element(nameof(this.Number)) ?? this.Number;
public override void WriteXml(XmlWriter writer)
writer.WriteStartElement(nameof(this.Title));
writer.WriteString(this.Title);
writer.WriteEndElement();
writer.WriteStartElement(nameof(this.Description));
writer.WriteString(this.Description);
writer.WriteEndElement();
writer.WriteStartElement(nameof(this.Number));
writer.WriteValue(this.Number);
writer.WriteEndElement();
public int Count { get => ChildItems.Count; }
public List<Child> ChildItems { get; set; } = new List<Child>();
public MainModel(bool createDemoContent) : this()
ChildItems.Add(new Child() { Id = 0, Nickname = DateTime.Now.ToLongDateString() });
System.Threading.Thread.Sleep(1);
ChildItems.Add(new Child() { Id = 1, Nickname = DateTime.Now.AddHours(-99).ToLongDateString() });
System.Threading.Thread.Sleep(1);
ChildItems.Add(new Child() { Id = 2, Nickname = DateTime.Now.AddDays(-99).ToLongDateString() });
public int Id { get; set; }
public string Nickname { get; set; }
public SerializableClass SerializableClass { get; set; } = new SerializableClass();
public string Title { get; set; }
public static void Test()
Test(XElement.Parse(xml).ToString(SaveOptions.None));
Test(XElement.Parse(xml).ToString(SaveOptions.DisableFormatting));
static void Test(string xml)
var mainModel = xml.LoadFromXml<MainModel>();
var xml2 = mainModel.GetXml();
Console.WriteLine("\nOriginal XML: ");
Console.WriteLine("\nDeserialized and re-serialized XML: ");
Assert.IsTrue(XNode.DeepEquals(XElement.Parse(xml), XElement.Parse(xml2)));
var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<MainModel xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
<Nickname>Freitag, 27. April 2018</Nickname>
<Title>Test title 1</Title>
<Description>Super description 1</Description>
<Nickname>Montag, 23. April 2018</Nickname>
<Title>Test title 2</Title>
<Description>Super description 2</Description>
<Nickname>Donnerstag, 18. Januar 2018</Nickname>
<Title>Test title 3</Title>
<Description>Super description 3</Description>
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 class AssertionFailedException : System.Exception
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
public static class Assert
public static void IsTrue(bool value)
public static void IsTrue(bool value, string message)
throw new AssertionFailedException(message ?? "failed");
public static void Main()
Console.WriteLine("Roslyn 2.0 Compiler; Environment version: " + Environment.Version);
Console.WriteLine("Uncaught exception: ");