using System.Collections.Generic;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
using System.ComponentModel;
using System.Diagnostics;
[XmlArray("largeImages")]
[XmlArrayItem("largeImage")]
public List<image> Images { get; set; } = new List<image>();
[XmlArray("smallImages")]
[XmlArrayItem("smallImage")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
public List<image> SmallImagesSurrogate { get { return Images; } }
public bool ShouldSerializeSmallImagesSurrogate() { return false; }
[XmlElement(ElementName="url")]
public string Url { get; set; }
[XmlElement(ElementName="height")]
public string Height { get; set; }
[XmlElement(ElementName="width")]
public string Width { get; set; }
public static void Test()
var root = xml.LoadFromXml<root>();
var xml2 = root.GetXml(true);
Console.WriteLine("Re-serialized {0}:", root);
Assert.IsTrue(XElement.Parse(xml).Descendants("url").Count() == root.Images.Count);
Assert.IsTrue(XElement.Parse(xml).Descendants("url").Select(e => (string)e).SequenceEqual(root.Images.Select(i => i.Url)));
Assert.IsTrue(XElement.Parse(xml).Descendants("width").Select(e => (string)e).SequenceEqual(root.Images.Select(i => i.Width)));
Assert.IsTrue(XElement.Parse(xml).Descendants("height").Select(e => (string)e).SequenceEqual(root.Images.Select(i => i.Height)));
Console.WriteLine("All tests passed.");
<url>./imageSmall.jpg</url>
<url>./imageSmall2.jpg</url>
<url>./imageLarge.jpg</url>
<url>./imageLarge2.jpg</url>
<url>./imageSmall3.jpg</url>
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: ");