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;
public static partial class JsonExtensions
public static JToken ToJToken(this XmlNode node, bool omitRootObject = false, string deserializeRootElementName = null, bool writeArrayAttribute = false)
var settings = new JsonSerializerSettings { Converters = { new XmlNodeConverter { OmitRootObject = omitRootObject, DeserializeRootElementName = deserializeRootElementName, WriteArrayAttribute = writeArrayAttribute } } };
var root = JToken.FromObject(node, JsonSerializer.CreateDefault(settings));
public static TJToken StripXmlPrefixCharacters<TJToken>(this TJToken root) where TJToken : JToken
return root.RenameProperties(s => s = s.TrimStart('@').TrimStart('#'));
public static TJToken RenameProperties<TJToken>(this TJToken root, Func<string, string> map) where TJToken : JToken
throw new ArgumentNullException();
return RenameReplaceProperty(root as JProperty, map, -1) as TJToken;
foreach (IList<JToken> obj in root.DescendantsAndSelf().OfType<JObject>())
for (int i = obj.Count - 1; i >= 0; i--)
RenameReplaceProperty((JProperty)obj[i], map, i);
public static IEnumerable<JToken> DescendantsAndSelf(this JToken node)
return Enumerable.Empty<JToken>();
var container = node as JContainer;
return container.DescendantsAndSelf();
private static JProperty RenameReplaceProperty(JProperty property, Func<string, string> map, int index)
var newName = map(property.Name);
if (newName == property.Name)
var value = property.Value;
var newProperty = new JProperty(newName, value);
IList<JToken> container = property.Parent;
index = container.IndexOf(property);
container[index] = newProperty;
public static void Test()
Console.WriteLine("\nInput XML: ");
var doc = new XmlDocument();
var myNode = doc.DocumentElement;
var token = myNode.ToJToken().StripXmlPrefixCharacters();
var jsonText = token.ToString(Newtonsoft.Json.Formatting.Indented);
Console.WriteLine("\nJSON after conversion: ");
Console.WriteLine(jsonText);
Console.WriteLine("\nJSON after conversion without stripping prefix characters: ");
Console.WriteLine(myNode.ToJToken());
var xml = @"<DataEntries>
<Data Name=""Direction"">in</Data>
<Data Name=""SourceAddress"">222.0.0.252</Data>
<Data Name=""SourcePort"">5355</Data>
<Data Name=""DestAddress"">192.168.1.24</Data>
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");