using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public void Test(string xmlString)
var xml = new XmlDocument();
Console.WriteLine("Original XML: ");
Console.WriteLine(GetXml());
Console.WriteLine("Resulting JSON: ");
Console.WriteLine("Newtonsoft.Json.Converters.XmlNodeConverter type information: ");
Console.WriteLine(typeof(Newtonsoft.Json.Converters.XmlNodeConverter).Assembly.FullName);
var old = JsonConvert.DefaultSettings;
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
Converters = { new FixedXmlNodeConverter() },
Console.WriteLine("Testing with global converter: ");
JsonConvert.DefaultSettings = old;
public String GetJson(XmlDocument xml)
return Newtonsoft.Json.JsonConvert.SerializeObject(xml, Newtonsoft.Json.Formatting.Indented);
string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<innerChildNode>Some Text</innerChildNode>
<!-- H:011 T:134ms S:1206 R:Tue May 02 07:59:03 PDT 2017 B:5.0.42642-master.3acb9f9~hotfix_pre.b72ccda -->
<!-- H:011 T:134ms S:1206 R:Tue May 02 07:59:03 PDT 2017 B:5.0.42642-master.3acb9f9~hotfix_pre.b72ccda -->";
public class FixedXmlNodeConverter : Newtonsoft.Json.Converters.XmlNodeConverter
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
base.WriteJson(writer, value, serializer);
writer.WriteComment("Global Comment: H:011 T:134ms S:1206 R:Tue May 02 07:59:03 PDT 2017 B:5.0.42642-master.3acb9f9~hotfix_pre.b72ccda");
public static void Main()
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);