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

Answer from David Brown on Stack Overflow
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ xml-to-json
Best XML to JSON Converter Online
XML to JSON is very unique tool for convert JOSN to XML and allows to download, save, share and print XML to JSON data.. XML to JSON support URL linking for sharing json. i.e. https://jsonformatter.org/xml-to-json/?url=https://gist.githubusercontent.com/jimmibond/8b75d0afcf249601174f1f504664072a/raw/c28e51ac26861d39f900d88e3c9fe2490374dbd9/xmlsample
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.

Discussions

[C#] How to properly convert this XML into valid JSON?
Why not deserialize the XML into an object using System.XML then serialize that object to JSON using System.Text.Json? XML deserialization can get pretty tricky, so it's better to use a well established library. More on reddit.com
๐ŸŒ r/learnprogramming
5
1
July 8, 2021
How do you guys parse and/or emit in JSON, XML, or YAML in C#?
.NET comes with very robust support for JSON and XML, never had to use yaml in .net though More on reddit.com
๐ŸŒ r/csharp
32
5
March 22, 2025
Tool to transform your test reports into JUnit format for better CI integration.
Cool, I remember looking at programmatic output of tests but ran into some problems. Mind contributing the test parsing logic to escargot for reuse by others? Mind splitting out your junit logic for reuse by others? EDIT: This reminds me that I need to set aside some time to dabble in Azure Pipelines for my rust projects. More on reddit.com
๐ŸŒ r/rust
9
13
January 27, 2019
XML to JSON cli tool?
As someone who has spent several years working intensively with both XML and JSON data, one of the problems is that there are multiple standards for how to represent XML data as JSON. This is because there are some structures in XML that can't be directly represented in JSON, so any conversion system must inevitably make some opinionated choices. Assuming that you also wish to have an opinion on what's the "correct" way to convert XML to JSON, your best bet is to write a simple converter tool using the XML and JSON serialisation libraries that are available in whichever programming language you prefer. More on reddit.com
๐ŸŒ r/commandline
24
16
June 16, 2019
๐ŸŒ
Code Beautify
codebeautify.org โ€บ xmltojson
XML to JSON Converter Online to convert XML to JSON String, URL and File
Convert XML to JSON helps to convert your XML data to JSON format. This tool allows loading the XML URL, which loads XML and turns into JSON String. Click on the URL button, Enter the URL and Submit.
๐ŸŒ
FreeFormatter
freeformatter.com โ€บ xml-to-json-converter.html
Free Online XML to JSON Converter - FreeFormatter.com
This free online tool lets you convert an XML file into a JSON file with your choice of indentation
๐ŸŒ
Altova
altova.com โ€บ xmlspy-xml-editor โ€บ xml-to-json
XML to JSON Converter | Altova
In addition to powerful conversion utilities, XMLSpy includes a JSON and XML editors, as well as support for working with XPath, XSLT, and XQuery alongside both technologies. You can convert a single file or perform a mass conversion on a large number of XML files instantly.
๐ŸŒ
CodeShack
codeshack.io โ€บ home โ€บ tools โ€บ xml to json converter
XML to JSON Converter
Convert your XML text or file into JSON format. Fast, free, and simple, all you need to do is enter valid XML text into the first text box.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ utilities โ€บ xml-to-json-converter
XML to JSON Converter - Free Online Tools %%page%% %%sep%% %%sitename%% - GeeksforGeeks
October 4, 2024 - The XML to JSON Converter tool facilitates seamless conversion of XML data structures into JSON format, aiding interoperability and compatibility between different data formats for enhanced data manipulation and integration capabilities.
๐ŸŒ
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 - Another method to install is: Click Tools=> NuGet Package Manager=> Package Manager Console. 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 converting XML to JSON.
๐ŸŒ
ReqBin
reqbin.com โ€บ xml-to-json
Online XML to JSON Converter
JSON is a format that originated from JavaScript but is now used by almost all programming languages, including PHP, Python, Java, C++, C#, and Go. JSON files have the *.json extension. ... Copy and paste the XML code into the left input box. Click ...
๐ŸŒ
Convertjson
convertjson.com โ€บ xml-to-json.htm
Convert XML to JSON
The tool will convert a XML file to JSON format.
๐ŸŒ
Site24x7
site24x7.com โ€บ tools โ€บ xml-to-json.html
XML to JSON Converter: Site24x7 Tools
Free tool which converts your XML data to its equivalent JSON format.The XML elements are converted to JSON keys and element values are transformed to corresponding JSON values.
๐ŸŒ
Visual Studio Marketplace
marketplace.visualstudio.com โ€บ items
XML to JSON - Visual Studio Marketplace
Extension for Visual Studio Code - Convert XML from clipboard or current document/selection to JSON
๐ŸŒ
JSONLint
jsonlint.com โ€บ xml-to-json
XML to JSON Converter - Transform XML Data Online | JSONLint | JSONLint
Convert XML to JSON instantly. Handles attributes, namespaces, and nested elements. Preview the result and copy or download the converted JSON.
๐ŸŒ
InterSystems
community.intersystems.com โ€บ post โ€บ how-convert-xml-json
How to convert from XML to JSON? | InterSystems Developer Community
December 8, 2020 - The key issue I see is: Is there a Related XML schema available. If YES: - you can generate a package with the existing tools - import the file with %XML.Reader - do an %JSON... export The XML schema is necessary because straight XML is just TEXT with no datatypes while JSON has data types.
๐ŸŒ
Newtonsoft
newtonsoft.com โ€บ json โ€บ help โ€บ html โ€บ ConvertJsonToXml.htm
Convert JSON to XML
This sample converts JSON to XML. Sample ยท Usage ยท Copy ยท string json = @"{ '@Id': 1, 'Email': 'james@example.com', 'Active': true, 'CreatedDate': '2013-01-20T00:00:00Z', 'Roles': [ 'User', 'Admin' ], 'Team': { '@Id': 2, 'Name': 'Software Developers', 'Description': 'Creators of fine software products and services.'
๐ŸŒ
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 - In this article, we have learned how to convert JSON to XML and XML to JSON in C#. We have explored the native ways, as well as the ways, using Newtonsoft.Json and our performance analysis shows that the latter one performs much better.
๐ŸŒ
Integrate.io
integrate.io โ€บ home โ€บ blog โ€บ conversions โ€บ how to convert xml to json: a step-by-step guide
How to Convert XML to JSON: A Step-by-Step Guide | Integrate.io
July 21, 2025 - This conversion is critical to ensure data interoperability, enhance system compatibility, and optimize data handling and storage. In this post, we guide you through the step-by-step process of converting XML to JSON.
๐ŸŒ
Code Beautify
codebeautify.org โ€บ jsontoxml
Best JSON to XML Converter to convert JSON to XML String, File, URL
Best Online JSON to XML Converter, Transformer Online Utility. Load form URL, Download, Save and Share.
๐ŸŒ
n8n
n8n.io โ€บ tools โ€บ xml-to-json
XML to JSON Converter - Free and Easy Conversion | n8n
To convert an XML document into JSON, just follow these steps: Choose how to input the XML file: either upload it or paste the text directly.