using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public static void Test()
var xml = @"<person xmlns:json='http://james.newtonking.com/projects/json' id='1' json:Array='true'>
<url>http://www.google.com</url>
<role json:Array='true'>Admin</role>
Console.WriteLine("Input XML: ");
Console.WriteLine(XDocument.Parse(xml));
static void TestXDocument(string xml)
var xDocument = XDocument.Parse(xml);
var json1 = JsonConvert.SerializeXNode(xDocument, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine("\nResult of converting XDocument to JSON: ");
Console.WriteLine(json1);
var json2 = JsonConvert.SerializeXNode(xDocument.Root, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine("\nResult of converting XDocument.Root to JSON: ");
Console.WriteLine(json2);
static void TestXmlDocument(string xml)
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
var json1 = JsonConvert.SerializeXmlNode(xmlDocument, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine("\nResult of converting XmlDocument to JSON: ");
Console.WriteLine(json1);
var json2 = JsonConvert.SerializeXmlNode(xmlDocument.DocumentElement, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine("\nResult of converting XmlDocument.DocumentElement to JSON: ");
Console.WriteLine(json2);
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: ");