Skip to content

Latest commit

 

History

History
57 lines (48 loc) · 1.42 KB

ConvertXmlToJsonForceArray.md

File metadata and controls

57 lines (48 loc) · 1.42 KB

Convert XML to JSON and force array

This sample reads the json:Array='true' attribute in the XML and places its value in an array when converting the XML to JSON.

var xml = """
          <person id='1'>
            <name>Alan</name>
            <url>http://www.google.com</url>
            <role>Admin1</role>
          </person>
          """;

var doc = new XmlDocument();
doc.LoadXml(xml);

var json = JsonXmlConvert.SerializeXmlNode(doc);

Console.WriteLine(json);
// {
//   "person": {
//     "@id": "1",
//     "name": "Alan",
//     "url": "http://www.google.com",
//     "role": "Admin1"
//   }
// }

xml = """
      <person xmlns:json='http://james.newtonking.com/projects/json' id='1'>
        <name>Alan</name>
        <url>http://www.google.com</url>
        <role json:Array='true'>Admin</role>
      </person>
      """;

doc = new();
doc.LoadXml(xml);

json = JsonXmlConvert.SerializeXmlNode(doc);

Console.WriteLine(json);
// {
//   "person": {
//     "@id": "1",
//     "name": "Alan",
//     "url": "http://www.google.com",
//     "role": [
//       "Admin"
//     ]
//   }
// }

snippet source | anchor