Converting between JSON and XML

Json.NET

Json.NET - Quick Starts & API Documentation Converting between JSON and XML

Json.NET supports converting JSON to XML and vice versa using the XmlNodeConverter.

Elements, attributes, text, comments, character data, processing instructions, namespaces and the XML declaration are all preserved when converting between the two. The only caveat is that it is possible to lose the order of differently named nodes at the same level when they are grouped together into an array.

Conversion Rules

  • Elements remain unchanged.
  • Attributes are prefixed with an @.
  • Single child text nodes are a value directly against an element, otherwise they are accessed via #text.
  • The XML declaration and processing instructions are prefixed with ?.
  • Charater data, comments, whitespace and significate whitespace nodes are accessed via #cdata-section, #comment, #whitespace and #significate-whitespace respectively.
  • Multiple nodes with the same name at the same level are grouped together into an array.
  • Empty elements are null.

SerializeXmlNode

The JsonConvert has two helper methods for converting between JSON and XML. The first is SerializeXmlNode. This method takes an XmlNode and serializes it to JSON text.

string xml = @"<?xml version=""1.0"" standalone=""no""?>
<root>
  <person id=""1"">
  <name>Alan</name>
  <url>http://www.google.com</url>
  </person>
  <person id=""2"">
  <name>Louis</name>
  <url>http://www.yahoo.com</url>
  </person>
</root>";
 
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
 
string jsonText = JsonConvert.SerializeXmlNode(doc);
//{
//  "?xml": {
//    "@version": "1.0",
//    "@standalone": "no"
//  },
//  "root": {
//    "person": [
//      {
//        "@id": "1",
//        "name": "Alan",
//        "url": "http://www.google.com"
//      },
//      {
//        "@id": "2",
//        "name": "Louis",
//        "url": "http://www.yahoo.com"
//      }
//    ]
//  }
//}

DeserializeXmlNode

The second helper method on JsonConvert is DeserializeXmlNode. This method takes JSON text and deserializes it into a XmlNode.

Because valid XML must have one root element the JSON passed to DeserializeXmlNode should have one property in the root JSON object. If the root JSON object has multiple properties then the overload that also takes an element name should be used. A root element with that name will be inserted into the deserialized XmlNode.

string json = @"{
  ""?xml"": {
    ""@version"": ""1.0"",
    ""@standalone"": ""no""
  },
  ""root"": {
    ""person"": [
      {
        ""@id"": ""1"",
        ""name"": ""Alan"",
        ""url"": ""http://www.google.com""
      },
      {
        ""@id"": ""2"",
        ""name"": ""Louis"",
        ""url"": ""http://www.yahoo.com""
      }
    ]
  }
}";
 
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
// <?xml version="1.0" standalone="no"?>
// <root>
//   <person id="1">
//   <name>Alan</name>
//   <url>http://www.google.com</url>
//   </person>
//   <person id="2">
//   <name>Louis</name>
//   <url>http://www.yahoo.com</url>
//   </person>
// </root>