Skip to content

Latest commit

 

History

History
50 lines (45 loc) · 1.2 KB

ConvertXmlToJson.md

File metadata and controls

50 lines (45 loc) · 1.2 KB

Convert XML to JSON

This sample converts XML to JSON.

var 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>
          """;

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

var json = JsonXmlConvert.SerializeXmlNode(doc);

Console.WriteLine(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"
//       }
//     ]
//   }
// }

snippet source | anchor