using System.Collections.Generic;
using System.Xml.Serialization;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Globalization;
using System.ComponentModel.DataAnnotations;
using System.Collections;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Threading.Tasks;
[XmlRootAttribute("MyClass", Namespace = "", IsNullable = false)]
set { comments = value; }
private System.Collections.Generic.List<string> tests = null;
public System.Collections.Generic.List<string> Tests
public string[] TestsArray
return (Tests == null ? null : Tests.ToArray());
(Tests = Tests ?? new List<string>(value.Length)).AddRange(value);
public static void Test()
foreach (var tuple in GetXml())
Console.WriteLine("Tests complete.");
static void Test(Tuple<string, List<string>> tuple)
Console.WriteLine("Input XML: ");
var myClass = xml.LoadFromXml<MyClass>();
Console.WriteLine("myClass.Tests = {0}", myClass.Tests == null ? "null" : myClass.Tests.Count.ToString());
Console.WriteLine("Re-serialized XML: ");
Console.WriteLine(myClass.GetXml(true));
Assert.IsTrue(ListsSame(myClass.Tests, tuple.Item2));
Console.WriteLine("Contents of myClass.Tests are correct.\n");
static bool ListsSame<T>(ICollection<T> list1, ICollection<T> list2)
if (object.ReferenceEquals(list1, list2))
else if (object.ReferenceEquals(list1, null) || object.ReferenceEquals(list2, null))
return list1.SequenceEqual(list2);
static IEnumerable<Tuple<string, List<string>>> GetXml()
<SomeNode>value</SomeNode>
</MyClass>", (List<string>)null),
</MyClass>", (List<string>)null),
<SomeNode>value</SomeNode>
</MyClass>", new List<string>()),
<SomeNode>value</SomeNode>
</MyClass>", new List<string>()),
<SomeNode>value</SomeNode>
</MyClass>", new List<string> { "hello", "goodbye" }),
public static class XmlSerializationHelper
public static T LoadFromXml<T>(this string xmlString, XmlSerializer serial = null)
serial = serial ?? new XmlSerializer(typeof(T));
T returnValue = default(T);
using (StringReader reader = new StringReader(xmlString))
object result = 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)
throw new AssertionFailedException("failed");
public static void Main()