🌐
Newtonsoft
newtonsoft.com › json › help › html › convertingjsonandxml.htm
Converting between JSON and XML
Empty elements are null. If the XML created from JSON doesn't match what you want, then you will need to convert it manually. The best approach to do this is to load your JSON into a LINQ to JSON object like JObject or JArray and then use LINQ to create an XDocument.
Discussions

JSON to XML converting with custom tag, JSON:array attribute is not added
I need to convert XML to JSON and again same JSON converts to XML so for that, I am using NewtonSoft.Json (Version=11.0.0.0) in my C# Code. I am using following code to convert JSON to XML. XmlDocu... More on github.com
🌐 github.com
3
April 29, 2018
c# - Newtonsoft.Json : How to convert json to xml when there are additional properties? - Stack Overflow
We create an xsd schema that matches a json generate a c# class from the xsd schema Use newtonsoft.json to deserialize the json to object and object to xml(based on the generated class) This is ne... More on stackoverflow.com
🌐 stackoverflow.com
How to convert JSON to XML or XML to JSON in C#? - Stack Overflow
Documentation here: Converting between JSON and XML with Json.NET ... Sign up to request clarification or add additional context in comments. ... I could not find this class. I use NewtonSoft Json.net 3.5. More on stackoverflow.com
🌐 stackoverflow.com
json.net - Converting XML to JSON array using Newtonsoft.Json - Stack Overflow
I have the following XML that I need to convert to the JSON shown below. I am using Newtonsoft.Json and cannot find the right combination of configuration and json:Array attributes to get what I n... More on stackoverflow.com
🌐 stackoverflow.com
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-xml-to-json-and-json-back-to-xml-using-newtonsoft-json
How to convert XML to Json and Json back to XML using Newtonsoft.json?
static void Main(string[] args) { string xml = @"Alanhttp://www.google1.com Admin1"; XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); string json = JsonConvert.SerializeXmlNode(doc); Console.WriteLine(json); Console.ReadLine(); } {"person":{"@id":"1","name":"Alan","url":"http://www.google1.com","role":"Admin1"}} 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); Console.ReadLine(); }
🌐
Couchbase
couchbase.com › home › converting xml to json in c# using json.net
Convert XML to JSON In C# Using Json.NET | Couchbase Guide
June 14, 2025 - Converting XML to JSON data that can be loaded into Couchbase Server can be accomplished with a little bit of .NET. Depending on the source of the data, you might be able to use a tool like Talend. But you may also want to write a simple C# .NET application with Newtonsoft’s Json.NET to do it.
🌐
eOne Solutions
eonesolutions.com › home › using newtonsoft with smartconnect to convert json to xml
Using Newtonsoft with SmartConnect to Convert JSON to XML
October 25, 2023 - This will convert my tech_tuesday.json file to xml and save an xml file called json_to_xml.xml. To complete the map setup, this xml file will already have to exist to be selected as the data source.
🌐
Code Maze
code-maze.com › home › how to convert json to xml or xml to json in c#
How to Convert JSON to XML or XML to JSON in C# - Code Maze
September 6, 2022 - Demonstrate how to convert JSON to XML back and forth using System.Text.Json and Newtonsoft.Json with detail explanation and examples
🌐
C# Corner
c-sharpcorner.com › blogs › how-to-convert-json-into-xml-or-xml-into-json
How To Convert JSON Into XML Or XML Into JSON
July 21, 2016 - Type “PM> Install-Package Newtonsoft.Json”. DeserializeXmlNode() is the helper method to convert JSON to XML. See the example, given below: SerializeXmlNode() is another helper method used for ...
🌐
GitHub
gist.github.com › jj09 › c401d7066a2df1437987d4803fffcdb6
Convert XML to JSON in C# · GitHub
Excellent example, recently I published similar article on Converting JSON to XML and Vice-versa in C#. Using "Newtonsoft.Json" and without using it also, might be helpful for developers, so here is the link Converting JSON to XML OR XML to JSON using C#
Find elsewhere
🌐
GitHub
github.com › JamesNK › Newtonsoft.Json › issues › 1734
JSON to XML converting with custom tag, JSON:array attribute is not added · Issue #1734 · JamesNK/Newtonsoft.Json
April 29, 2018 - I need to convert XML to JSON and again same JSON converts to XML so for that, I am using NewtonSoft.Json (Version=11.0.0.0) in my C# Code. I am using following code to convert JSON to XML. XmlDocument xmlDoc = JsonConvert.DeserializeXml...
Author   vatsaldesai
🌐
GitHub
github.com › CheckPointSW › SmartMove › blob › master › packages › Newtonsoft.Json.8.0.3 › lib › net45 › Newtonsoft.Json.xml
SmartMove/packages/Newtonsoft.Json.8.0.3/lib/net45/Newtonsoft.Json.xml at master · CheckPointSW/SmartMove
Gets or sets the name of the root element to insert when deserializing to XML if the JSON structure has produces multiple root elements. ... <value><c>true</c> if the array attibute is written to the XML; otherwise, <c>false</c>.</value> ...
Author   CheckPointSW
🌐
Newtonsoft
newtonsoft.com › json › help › html › ConvertXmlToJsonForceArray.htm
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.
🌐
Stack Overflow
stackoverflow.com › questions › 64863424 › newtonsoft-json-how-to-convert-json-to-xml-when-there-are-additional-propertie
c# - Newtonsoft.Json : How to convert json to xml when there are additional properties? - Stack Overflow
I went to Google and searched for site:stackoverflow.com c# serialize dictionary to xml and found "about 9,880 results", one of which is How to XML-serialize a dictionary. This is programming; take each problem, search for an answer, continue as needed. ... :| I was on the same link and trying it out. But had an issue that's why posted. But I get your point, a little more persistence :)Thanks for the directions. ... Newtonsoft provides a method for doing exactly that. var node = JsonConvert.DeserializeXNode(jsonString, "Root");
Top answer
1 of 12
499

Yes. Using the JsonConvert class which contains helper methods for this precise purpose:

// 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 node
XmlDocument doc = JsonConvert.DeserializeXmlNode(json);

Documentation here: Converting between JSON and XML with Json.NET

2 of 12
60

Yes, you can do it (I do) but Be aware of some paradoxes when converting, and handle appropriately. You cannot automatically conform to all interface possibilities, and there is limited built-in support in controlling the conversion- many JSON structures and values cannot automatically be converted both ways. Keep in mind I am using the default settings with Newtonsoft JSON library and MS XML library, so your mileage may vary:

XML -> JSON

  1. All data becomes string data (for example you will always get "false" not false or "0" not 0) Obviously JavaScript treats these differently in certain cases.
  2. Children elements can become nested-object {} OR nested-array [ {} {} ...] depending if there is only one or more than one XML child-element. You would consume these two differently in JavaScript, etc. Different examples of XML conforming to the same schema can produce actually different JSON structures this way. You can add the attribute json:Array='true' to your element to workaround this in some (but not necessarily all) cases.
  3. Your XML must be fairly well-formed, I have noticed it doesn't need to perfectly conform to W3C standard, but 1. you must have a root element and 2. you cannot start element names with numbers are two of the enforced XML standards I have found when using Newtonsoft and MS libraries.
  4. In older versions, Blank elements do not convert to JSON. They are ignored. A blank element does not become "element":null

A new update changes how null can be handled (Thanks to Jon Story for pointing it out): https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_NullValueHandling.htm

JSON -> XML

  1. You need a top level object that will convert to a root XML element or the parser will fail.
  2. Your object names cannot start with a number, as they cannot be converted to elements (XML is technically even more strict than this) but I can 'get away' with breaking some of the other element naming rules.

Please feel free to mention any other issues you have noticed, I have developed my own custom routines for preparing and cleaning the strings as I convert back and forth. Your situation may or may not call for prep/cleanup. As StaxMan mentions, your situation may actually require that you convert between objects...this could entail appropriate interfaces and a bunch of case statements/etc to handle the caveats I mention above.

🌐
Newtonsoft
newtonsoft.com › json › help › html › M_Newtonsoft_Json_JsonConvert_DeserializeXmlNode_3.htm
JsonConvert.DeserializeXmlNode Method (String, String, Boolean, Boolean)
Namespace: Newtonsoft.Json Assembly: Newtonsoft.Json (in Newtonsoft.Json.dll) Version: 12.0.1+509643a8952ce731e0207710c429ad6e67dc43db ... public static XmlDocument DeserializeXmlNode( string value, string deserializeRootElementName, bool writeArrayAttribute, bool encodeSpecialCharacters ) ... Type: SystemString The JSON string. ... Type: SystemString The name of the root element to append when deserializing.
Top answer
1 of 1
4

json:Array='true' isn't going to give you the result you want in this situation. This attribute is intended to signal that you want a particular XML element to be treated as an array when it is converted to JSON in the case that there is only one of them. If there are multiple of a particular XML element, they will automatically be treated as an array. So in your case, the multiple Encounter elements will become an array inside the single Encounters object. It looks like you actually want to flatten this down one level. Json.Net does not have a way to do this using attributes in the XML. So there are three possible approaches I can see to solve this:

  1. Manipulate the XML before converting it to JSON
  2. Manipulate the JSON after converting it from XML
  3. Write a custom XML-to-JSON converter to do what you want.

I think approach 2 is probably the easiest using the LINQ-to-JSON API (JObjects):

string xml =
@"<Container>
  <Encounters>
    <Encounter>
      <a>a1</a>
      <b>b1</b>
    </Encounter>
    <Encounter>
      <a>a2</a>
      <b>b2</b>
    </Encounter>
  </Encounters>
</Container>";

// Convert XML to a JObject
JObject root = JObject.Parse(JsonConvert.SerializeXNode(XElement.Parse(xml)));

// Move the array of Encounter objects up one level
JProperty encounters = ((JObject)root["Container"]).Property("Encounters");
encounters.Value = encounters.Value["Encounter"];

// Output the JSON
string json = root.ToString();
Console.WriteLine(json);

Output:

{
  "Container": {
    "Encounters": [
      {
        "a": "a1",
        "b": "b1"
      },
      {
        "a": "a2",
        "b": "b2"
      }
    ]
  }
}

Fiddle: https://dotnetfiddle.net/kk1Exr

🌐
Code Beautify
codebeautify.org › blog › how-to-convert-json-to-xml-using-c
How to Convert JSON to XML Using C#
January 1, 2026 - JSON (JavaScript Object Notation) and XML (eXtensible Markup Language) are two widely used formats for data representation. In this tutorial, we’ll explore how to convert JSON to XML using C#. ... Start by opening Visual Studio and creating a new C# console application project. Name it “JSONtoXMLConverter”. We’ll use the Newtonsoft.Json library for JSON manipulation.
🌐
Binaryintellect
binaryintellect.net › articles › d56c7798-703d-45cf-be74-a8b0cec94a3c.aspx
JSON to XML / XML to JSON conversion in .NET Core | Bipin Joshi .NET
You can either write your own parsing logic or use some third-party library such as Json.NET to accomplish such a conversion. In the second approach you first deserialize XML data into a C# object and then serialize the C# object to JSON. If you wish to convert from JSON to XML similar process will be followed - JSON to C# object and C# object to XML.