using System.Collections.Generic;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
[System.Xml.Serialization.XmlRootAttribute(IsNullable = false)]
public class GeneralInformation
private Info[] addInfoList;
public Info[] AddInfoList
this.addInfoList = value;
const string InfoPrefix = "Info";
const string InfoListPrefix = "InfoList";
[XmlAnyElement("InfoList")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
public XElement AddInfoListXml
return new XElement(InfoListPrefix,
.Select((info, i) => new KeyValuePair<string, Info>(InfoPrefix + (i + 1).ToString("D3", NumberFormatInfo.InvariantInfo), info))
.SerializeToXElements((XNamespace)""));
.Where(e => e.Name.LocalName.StartsWith(InfoPrefix))
.DeserializeFromXElements<Info>()
private string infoMessage;
[System.Xml.Serialization.XmlElement("InfoName")]
public string InfoMessage
this.infoMessage = value;
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 });
public static void Test()
var inputXml = GetInputXml();
Test<GeneralInformation>(inputXml);
public static void Test<T>(string inputXml)
Console.WriteLine("Input XML: ");
Console.WriteLine(inputXml);
var root = inputXml.LoadFromXml<T>();
var outputXml = root.GetXml();
Console.WriteLine("Deserialized and re-serialized XML for {0}: ", root);
Console.WriteLine(outputXml);
Assert.IsTrue(XNode.DeepEquals(XElement.Parse(inputXml), XElement.Parse(outputXml)));
Console.WriteLine("\nInput and output XML are equivalent.");
static string GetInputXml()
return @"<GeneralInformation xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
<InfoName>Test1</InfoName>
<InfoName>Test2</InfoName>
<InfoName>Test3</InfoName>
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: ");