A .NET library for serializing XML to JSON and deserializing it back, depending only on System.Text.Json.
This library is designed to be a drop-in replacement for Newtonsoft.Json.Converters.XmlNodeConverter.
using System.Text.Json;
using System.Xml;
using JsonXml;
var xml = @"<root><child>First</child><child attribute=""value"">Second</child></root>";
var document = new XmlDocument();
document.LoadXml(xml);
var options = new JsonSerializerOptions
{
Converters = { new XmlNodeConverter() }
};
var json = JsonSerializer.Serialize(document, options);
Console.WriteLine(json);
// {"root":{"child":["First",{"@attribute":"value","#text":"Second"}]}}
Console.WriteLine(JsonSerializer.Deserialize<XmlDocument>(json, options)!.OuterXml == xml);
// TrueJsonXmldoes not make use of a customjsonnamespace for controlling array serializationJsonXmldoes not make use of special$-properties
- Implement handling entity references (
Newtonsoft.Jsonfails) - Make a NuGet package