using System.Collections.Generic;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
public class ItemProperty : IXmlSerializable
public static ItemProperty Empty { get; } = new ItemProperty();
this.Name = string.Empty;
this.Value = string.Empty;
public string Name { get; set; }
public string Value { get; set; }
public int Id { get; set; }
[XmlAttribute("itemOrder")]
public int Order { get; set; }
public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
this.Name = reader.LocalName;
if (reader.HasAttributes)
var id = reader.GetAttribute("id");
this.Id = XmlConvert.ToInt32(id);
var order = reader.GetAttribute("itemOrder");
this.Order = XmlConvert.ToInt32(order);
string sequence = reader.GetAttribute("seq");
this.Value = reader.ReadElementContentAsString();
public void WriteXml(XmlWriter writer)
throw new NotImplementedException();
public virtual ItemProperty ItemNumber { get; set; } = ItemProperty.Empty;
public virtual ItemProperty Category { get; set; } = ItemProperty.Empty;
[XmlElement("EmptyElement")]
public virtual ItemProperty EmptyElement { get; set; } = ItemProperty.Empty;
[XmlElement("NonEmptyElement")]
public virtual ItemProperty NonEmptyElement { get; set; } = ItemProperty.Empty;
[XmlAttribute("totalExecutionTime")]
public string ExecutionTime { get; set; }
[XmlElement("ItemNumber", Type = typeof(ItemBase))]
public List<ItemBase> Items { get; set; }
public static void Test()
Console.WriteLine("Input XML: ");
Console.WriteLine(XElement.Parse(xml));
var data = xml.LoadFromXml<ItemData>();
Assert.IsTrue(data.Items.Count == 1);
Assert.IsTrue(data.Items[0].ItemNumber.Name == "Value");
Assert.IsTrue(data.Items[0].Category.Name == "Category");
Assert.IsTrue(data.Items[0].EmptyElement.Name == "EmptyElement");
Assert.IsTrue(data.Items[0].NonEmptyElement.Name == "NonEmptyElement");
var xml = @"<Data totalExecutionTime=""00:00:00.0467241"">
<ItemNumber id=""1234"" order=""0"" createdDate=""2017-03-24T12:07:09.07"" modifiedDate=""2018-08-29T16:59:19.127"">
<!-- Comment --><Value modifiedDate=""2017-03-24T12:07:12.77"">ABC1234</Value>
<NonEmptyElement id=""3"" parentID=""9876"" itemOrder=""3"" modifiedDate=""2017-03-24T12:16:23.687"">AA<![CDATA[ <&&> some cdata <&&> ]]>BB</NonEmptyElement>
<EmptyElement id=""2"" parentID=""9876"" itemOrder=""2"" modifiedDate=""2017-03-24T12:16:23.687"" />
<Category id=""5432"" parentID=""9876"" itemOrder=""1"" modifiedDate=""2017-03-24T12:16:23.687"">The best category</Category>
... <!-- like 100 other elements -->
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 string GetOuterXml(this XmlNode node, bool indent = true)
using (var textWriter = new StringWriter())
var settings = new XmlWriterSettings
OmitXmlDeclaration = true,
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
return textWriter.ToString();
public static class XmlAssert
public static void AreEqual(
Assert.IsTrue(XNode.DeepEquals(Normalize(expected), Normalize(actual)));
private static XElement Normalize(XElement element)
.OrderBy(a => a.Name.ToString()),
.OrderBy(a => a.Name.ToString())
.Select(e => Normalize(e)));
.OrderBy(a => a.Name.ToString()));
return new XElement(element.Name, element.Attributes()
.OrderBy(a => a.Name.ToString()), element.Value);
public static void Main()
Console.WriteLine("Roslyn 2.0 Compiler; Environment version: " + Environment.Version);
Console.WriteLine("Uncaught exception: ");