CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Xml.Linq;
using System.Reflection;
using System.Data;
using System.Xml.Schema;
using System.Text.RegularExpressions;
using System.Net;
using System.Xml.XPath;
using System.Globalization;
using System.ComponentModel.DataAnnotations;
using System.Collections;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Threading.Tasks;
// https://stackoverflow.com/questions/45358844/c-sharp-xml-serializer-deserializes-list-to-count-of-0-instead-of-null
[XmlRootAttribute("MyClass", Namespace = "", IsNullable = false)]
public class MyClass
{
private string comments;
public string Comments
{
set { comments = value; }
get { return comments; }
}
private System.Collections.Generic.List<string> tests = null;
[XmlIgnore]
public System.Collections.Generic.List<string> Tests
{
get { return tests; }
set { tests = value; }
}
[XmlArray("Tests")]
public string[] TestsArray
{
get
{
return (Tests == null ? null : Tests.ToArray());
}
set
{
if (value == null)
return;
(Tests = Tests ?? new List<string>(value.Length)).AddRange(value);
}
}
}
class TestClass
{
public static void Test()
{
foreach (var tuple in GetXml())
Test(tuple);
Console.WriteLine("Tests complete.");
}
static void Test(Tuple<string, List<string>> tuple)
{
var xml = tuple.Item1;
Console.WriteLine("Input XML: ");
Console.WriteLine(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))
return true;
else if (object.ReferenceEquals(list1, null) || object.ReferenceEquals(list2, null))
return false;
return list1.SequenceEqual(list2);
}
static IEnumerable<Tuple<string, List<string>>> GetXml()
{
return new[]
{
Tuple.Create(@"<MyClass>
<SomeNode>value</SomeNode>
</MyClass>", (List<string>)null),
Tuple.Create(@"<MyClass>
</MyClass>", (List<string>)null),
Tuple.Create(@"<MyClass>
<SomeNode>value</SomeNode>
<Tests />
</MyClass>", new List<string>()),
Tuple.Create(@"<MyClass>
<SomeNode>value</SomeNode>
<Tests></Tests>,
</MyClass>", new List<string>()),
Tuple.Create(@"<MyClass>
<SomeNode>value</SomeNode>
<Tests>
<string>hello</string>
<string>goodbye</string>
</Tests>
</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);
if (result is T)
{
returnValue = (T)result;
}
}
return returnValue;
}
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();
ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
}
using (var textWriter = new StringWriter())
{
var settings = new XmlWriterSettings() { Indent = true }; // For cosmetic purposes.
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)
{
if (value == false)
throw new AssertionFailedException("failed");
}
}
public class Program
{
public static void Main()
{
TestClass.Test();
}
}