Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert XML to Json and Json back to XML using Newtonsoft.json?
Newtonsoft.Json (Json.NET) provides powerful methods for converting between XML and JSON formats. The library preserves all XML features including elements, attributes, text content, comments, character data, processing instructions, namespaces, and XML declarations during conversion.
Required NuGet Package
Install the Newtonsoft.Json package to use these conversion methods −
Install-Package Newtonsoft.Json
Key Methods
The JsonConvert class provides two essential methods for XML-JSON conversion −
- SerializeXmlNode() − Converts XML to JSON format
- DeserializeXmlNode() − Converts JSON back to XML format
Converting XML to JSON
Use SerializeXmlNode() to convert an XmlDocument or XmlNode to JSON string −
Example
using System;
using System.Xml;
using Newtonsoft.Json;
class Program {
static void Main(string[] args) {
string xml = @"<person id='1'>
<name>Alan</name>
<url>http://www.google1.com</url>
<role>Admin1</role>
</person>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string json = JsonConvert.SerializeXmlNode(doc);
Console.WriteLine("XML to JSON:");
Console.WriteLine(json);
}
}
The output of the above code is −
XML to JSON:
{"person":{"@id":"1","name":"Alan","url":"http://www.google1.com","role":"Admin1"}}
Converting JSON to XML
Use DeserializeXmlNode() to convert JSON string back to XmlDocument −
Example
using System;
using System.Xml;
using Newtonsoft.Json;
class Program {
static void Main(string[] args) {
string json = @"{
'?xml': {
'@version': '1.0',
'@standalone': 'no'
},
'root': {
'person': [
{
'@id': '1',
'name': 'Alan',
'url': 'http://www.google1.com'
},
{
'@id': '2',
'name': 'Louis',
'url': 'http://www.yahoo1.com'
}
]
}
}";
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
Console.WriteLine("JSON to XML:");
Console.WriteLine(doc.OuterXml);
}
}
The output of the above code is −
JSON to XML: <?xml version="1.0" standalone="no"?><root><person id="1"><name>Alan</name><url>http://www.google1.com</url></person><person id="2"><name>Louis</name><url>http://www.yahoo1.com</url></person></root>
Complete Conversion Example
This example demonstrates converting XML to JSON and then back to XML −
Example
using System;
using System.Xml;
using Newtonsoft.Json;
class Program {
static void Main(string[] args) {
// Original XML
string originalXml = @"<employees>
<employee id='101'>
<name>John Doe</name>
<department>IT</department>
</employee>
<employee id='102'>
<name>Jane Smith</name>
<department>HR</department>
</employee>
</employees>";
// Load XML
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(originalXml);
// Convert XML to JSON
string jsonResult = JsonConvert.SerializeXmlNode(xmlDoc, Formatting.Indented);
Console.WriteLine("Converted to JSON:");
Console.WriteLine(jsonResult);
// Convert JSON back to XML
XmlDocument reconstructedXml = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonResult);
Console.WriteLine("\nConverted back to XML:");
Console.WriteLine(reconstructedXml.OuterXml);
}
}
The output of the above code is −
Converted to JSON:
{
"employees": {
"employee": [
{
"@id": "101",
"name": "John Doe",
"department": "IT"
},
{
"@id": "102",
"name": "Jane Smith",
"department": "HR"
}
]
}
}
Converted back to XML:
<employees><employee id="101"><name>John Doe</name><department>IT</department></employee><employee id="102"><name>Jane Smith</name><department>HR</department></employee></employees>
Key Conversion Rules
| XML Feature | JSON Representation |
|---|---|
| XML Attributes | Prefixed with @ symbol (@id, @version) |
| XML Declaration | Represented as ?xml object |
| Multiple Elements | Converted to JSON arrays |
| Text Content | Direct string values |
Conclusion
Newtonsoft.Json provides seamless conversion between XML and JSON using SerializeXmlNode() and DeserializeXmlNode() methods. The conversion preserves all XML structure and attributes, making it perfect for data interchange between XML and JSON-based systems.
