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 DeactivationsXml
public DeactivationsGroup[] Groups { get; set; }
public class DeactivationsGroup
public DeactivationsGroup() { this.GroupNames = new Dictionary<string, GroupName>(); }
public Dictionary<string, GroupName> GroupNames { get; set; }
public int Level { get; set; }
public byte index { get; set; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
public XElement[] XmlGroupNames
return GroupNames.SerializeToXElements(null);
if (value == null || value.Length < 1)
foreach (var pair in value.DeserializeFromXElements<GroupName>())
GroupNames.Add(pair.Key, pair.Value);
[XmlElement("GroupName")]
public string Name { get; set; }
public static class XmlKeyValueListHelper
const string RootLocalName = "Root";
public static XElement [] SerializeToXElements<T>(this IEnumerable<KeyValuePair<string, T>> dictionary, XNamespace ns)
var serializer = XmlSerializerFactory.Create(typeof(T), RootLocalName, ns.NamespaceName);
.Select(p => new { p.Key, Value = p.Value.SerializeToXElement(serializer, true) })
.Select(p => new XElement(ns + p.Key, p.Value.Attributes().Where(a => !a.IsNamespaceDeclaration), p.Value.Elements()))
public static IEnumerable<KeyValuePair<string, T>> DeserializeFromXElements<T>(this IEnumerable<XElement> elements)
XmlSerializer serializer = null;
foreach (var element in elements)
if (serializer == null || element.Name.Namespace != ns)
ns = element.Name.Namespace;
serializer = XmlSerializerFactory.Create(typeof(T), RootLocalName, ns.NamespaceName);
var elementToDeserialize = new XElement(ns + RootLocalName, element.Attributes(), element.Elements());
yield return new KeyValuePair<string, T>(element.Name.LocalName, elementToDeserialize.Deserialize<T>(serializer));
public static XmlSerializerNamespaces NoStandardXmlNamespaces()
var ns = new XmlSerializerNamespaces();
public static XElement SerializeToXElement<T>(this T obj)
return obj.SerializeToXElement(null, NoStandardXmlNamespaces());
public static XElement SerializeToXElement<T>(this T obj, XmlSerializerNamespaces ns)
return obj.SerializeToXElement(null, ns);
public static XElement SerializeToXElement<T>(this T obj, XmlSerializer serializer, bool omitStandardNamespaces)
return obj.SerializeToXElement(serializer, (omitStandardNamespaces ? NoStandardXmlNamespaces() : null));
public static XElement SerializeToXElement<T>(this T obj, XmlSerializer serializer, XmlSerializerNamespaces ns)
var doc = new XDocument();
using (var writer = doc.CreateWriter())
(serializer ?? new XmlSerializer(obj.GetType())).Serialize(writer, obj, ns);
public static T Deserialize<T>(this XContainer element, XmlSerializer serializer)
using (var reader = element.CreateReader())
object result = (serializer ?? new XmlSerializer(typeof(T))).Deserialize(reader);
public static class XmlSerializerFactory
readonly static Dictionary<Tuple<Type, string, string>, XmlSerializer> cache;
readonly static object padlock;
static XmlSerializerFactory()
cache = new Dictionary<Tuple<Type, string, string>, XmlSerializer>();
public static XmlSerializer Create(Type serializedType, string rootName, string rootNamespace)
if (serializedType == null)
throw new ArgumentNullException();
if (rootName == null && rootNamespace == null)
return new XmlSerializer(serializedType);
XmlSerializer serializer;
var key = Tuple.Create(serializedType, rootName, rootNamespace);
if (!cache.TryGetValue(key, out serializer))
cache[key] = serializer = new XmlSerializer(serializedType, new XmlRootAttribute { ElementName = rootName, Namespace = rootNamespace });
string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
" <GroupName>ANTRIEB</GroupName>" +
" <GroupName>missing translation!</GroupName>" +
" <GroupName>BREMSEN</GroupName>" +
XmlSerializer serializer = new XmlSerializer(typeof(DeactivationsXml));
using (TextReader fileStream = new StringReader(xml))
var result = (DeactivationsXml)serializer.Deserialize(fileStream);
var newXml = result.GetXml();
ConsoleAndDebug.WriteLine("Deserialized and re-serialized XML: ");
ConsoleAndDebug.WriteLine(newXml);
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 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);