Converting JSON to XML Converting JSON to XML xml xml

Converting JSON to XML


Despite the fact your JSON provided in the question is not complete, you have multiple properties at the top level as indicated by the exception. You have to define the root for it to get valid XML:

var doc = JsonConvert.DeserializeXmlNode(jsonOutput, "root");

EDIT: In order to print out your XML with indentation you can use XDocument class from System.Xml.Linq namespace: XDocument.Parse(doc.InnerXml).


I thought it's worth linking to the Documentation for turning xml to json and the other way around.

The guys are right..

// To convert an XML node contained in string xml into a JSON string   XmlDocument doc = new XmlDocument();doc.LoadXml(xml);string jsonText = JsonConvert.SerializeXmlNode(doc);// To convert JSON text contained in string json into an XML nodeXmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);


You can do JSON-to-XML also by using the .NET Framework (System.Runtime.Serialization.Json):

private static XDocument JsonToXml(string jsonString){    using (var stream = new MemoryStream(Encoding.ASCII.GetBytes(jsonString)))    {        var quotas = new XmlDictionaryReaderQuotas();        return XDocument.Load(JsonReaderWriterFactory.CreateJsonReader(stream, quotas));    }}