using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
[Newtonsoft.Json.JsonConverter(typeof(FooListResponseConverter))]
[Serializable, XmlType(AnonymousType = true), XmlRoot(Namespace = "", ElementName = "FooList", IsNullable = false)]
public class FooListResponse
[XmlElement("Foo", IsNullable = false)]
public List<Foo> FooList { get; set; }
[XmlElement("Name"), JsonProperty("Name")]
public string Name { get; set; }
[XmlElement("Color"), JsonProperty("Color")]
public string Color { get; set; }
public class FooListResponseConverter : JsonConverter<FooListResponse>
public override void WriteJson(JsonWriter writer, FooListResponse value, JsonSerializer serializer)
var list = value == null ? null : value.FooList;
writer.WriteStartArray();
writer.WriteStartObject();
foreach (var foo in list)
writer.WritePropertyName("Foo");
serializer.Serialize(writer, foo);
class FooListDeserializationDto
[JsonIgnore] public readonly List<Foo> FooList = new List<Foo>();
public Foo Foo { set { FooList.Add(value); } }
public override FooListResponse ReadJson(JsonReader reader, Type objectType, FooListResponse existingValue, bool hasExistingValue, JsonSerializer serializer)
var dtoList = serializer.Deserialize<List<FooListDeserializationDto>>(reader);
var list = dtoList.Count == 1
: dtoList.SelectMany(d => d.FooList).ToList();
var fooListResponse = existingValue ?? new FooListResponse();
fooListResponse.FooList = list;
public static void Test()
var fooListResponse = inputXml.LoadFromXml<FooListResponse>();
var json = JsonConvert.SerializeObject(fooListResponse, Newtonsoft.Json.Formatting.Indented);
var fooListResponse2 = JsonConvert.DeserializeObject<FooListResponse>(json);
var json2 = JsonConvert.SerializeObject(fooListResponse2, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine("Re-serialized {0}:", fooListResponse);
Assert.Equal(json, json2);
Assert.Equal(fooListResponse.FooList.Count, 2);
Assert.True(json2.ReplaceWhitespace() == GetRequiredJson().ReplaceWhitespace());
public static void TestXml()
var fooListResponse = inputXml.LoadFromXml<FooListResponse>();
var xml = fooListResponse.GetXml(omitStandardNamespaces : true);
Console.WriteLine("Re-serialized {0}:", fooListResponse);
Assert.Equal(XNode.DeepEquals(XElement.Parse(xml), XElement.Parse(inputXml)), true);
Assert.Equal(fooListResponse.FooList.Count, 2);
return @"<?xml version=""1.0"" encoding=""utf-8""?>
static string GetRequiredJson()
public static class StringExtensions
private static readonly Regex sWhitespace = new Regex(@"\s+");
public static string ReplaceWhitespace(this string input, string replacement = "")
return sWhitespace.Replace(input, replacement);
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, 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);
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Namespace, typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");