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;
public class MyCollection : System.Collections.Generic.Dictionary<string, string>, IXmlSerializable
public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
using (var subReader = reader.ReadSubtree())
XmlKeyValueListHelper.ReadKeyValueXml(subReader, this);
public void WriteXml(System.Xml.XmlWriter writer)
XmlKeyValueListHelper.WriteKeyValueXml(writer, this);
public static class XmlKeyValueListHelper
private const string XmlElementName = "MyData";
private const string XmlAttributeId = "Id";
public static void WriteKeyValueXml(System.Xml.XmlWriter writer, ICollection<KeyValuePair<string, string>> collection)
foreach (var pair in collection)
writer.WriteStartElement(XmlElementName);
writer.WriteAttributeString(XmlAttributeId, pair.Key);
writer.WriteString(pair.Value);
writer.WriteEndElement();
public static void ReadKeyValueXml(System.Xml.XmlReader reader, ICollection<KeyValuePair<string, string>> collection)
if (reader.IsEmptyElement)
reader.ReadStartElement();
while (reader.NodeType != XmlNodeType.EndElement)
if (reader.NodeType == XmlNodeType.Element && reader.LocalName == XmlElementName)
var tag = reader.GetAttribute(XmlAttributeId);
if (reader.IsEmptyElement)
content = reader.ReadElementContentAsString();
collection.Add(new KeyValuePair<string, string>(tag, content));
public MyCollection MyCollection { get; set; }
public string ZValue { get; set; }
public static void Test()
TestRead(new MyCollection { {"", ""}, { "1", "some content" }, { "2", "some other content" } }, true);
TestRead(new MyCollection { {"", ""}, { "1", "some content" }, { "2", "some other content" } }, false);
TestRead(new MyCollection { }, false);
ConsoleAndDebug.WriteLine("Done");
static void TestRead(MyCollection old, bool addSpoiler)
var root = new Root { MyCollection = old, ZValue = "zvalue" };
var document = XDocument.Parse(root.GetXml());
var child = document.Root.Element("MyCollection").Elements().FirstOrDefault();
child.AddBeforeSelf(new XComment("A comment"));
child.AddBeforeSelf(new XText("spoiler"));
child.AddAfterSelf(new XElement("foo", new XElement("bar", "some bar text")));
Console.WriteLine("Testing XML: ");
Console.WriteLine(document);
var xmlIndented = document.ToString(SaveOptions.None);
var xmlUnindented = document.ToString(SaveOptions.DisableFormatting);
TestRead(root, xmlUnindented);
TestRead(root, xmlIndented);
static void TestRead(Root old, string xml)
var newRoot = xml.LoadFromXml<Root>();
Assert.IsTrue(newRoot.MyCollection.Count == old.MyCollection.Count);
Assert.IsTrue(newRoot.ZValue == old.ZValue);
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 class ConsoleAndDebug
public static void WriteLine(object s)
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 static void Main()
Console.WriteLine("Environment version: " + Environment.Version);