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;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public static class JsonExtensions
const string JsonNamespace = @"http://james.newtonking.com/projects/json";
const string ArrayAttributeName = @"Array";
public static JToken ToJToken(this XElement xElement, bool omitRootObject, string deserializeRootElementName)
return xElement.ToJToken(omitRootObject, deserializeRootElementName, Enumerable.Empty<Func<XElement, IEnumerable<XElement>>>());
public static JToken ToJToken(this XElement xElement, bool omitRootObject, string deserializeRootElementName, IEnumerable<Func<XElement, IEnumerable<XElement>>> arrayQueries)
foreach (var query in arrayQueries)
var name = XName.Get(ArrayAttributeName, JsonNamespace);
foreach (var element in query(xElement))
element.SetAttributeValue(name, true);
var settings = new JsonSerializerSettings { Converters = { new XmlNodeConverter { OmitRootObject = omitRootObject, DeserializeRootElementName = deserializeRootElementName } } };
var root = JToken.FromObject(xElement, JsonSerializer.CreateDefault(settings));
public string Name { get; set; }
public string URL { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
public List<Person> Person { get; set; }
public static void Test()
string xml = @"<?xml version='1.0' standalone='no'?>
<url>http://www.google.com</url>
var xElement = XElement.Parse(xml);
var jToken = xElement.ToJToken(true, "",
new Func<XElement, IEnumerable<XElement>> [] { e => e.Elements().Where(i => string.Equals(i.Name.LocalName, "Person", StringComparison.OrdinalIgnoreCase)) });
var root = jToken.ToObject<Root>();
Console.WriteLine("\nInput XML: ");
Console.WriteLine("\nIntermediate JSON: ");
Console.WriteLine(jToken);
Console.WriteLine(string.Format("\nDeserialized {0} re-serialized to XML: ", root));
Console.WriteLine(root.GetXml());
Assert.IsTrue(root.Person.Count == 1);
public static void Main()
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
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 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();