
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to convert XML to Json and Json back to XML using Newtonsoft.json?
Json.NET supports converting JSON to XML and vice versa using the XmlNodeConverter.
Elements, attributes, text, comments, character data, processing instructions, namespaces, and the XML declaration are all preserved when converting between the two
SerializeXmlNode
The JsonConvert has two helper methods for converting between JSON and XML. The first is SerializeXmlNode(). This method takes an XmlNode and serializes it to JSON text.
DeserializeXmlNode
The second helper method on JsonConvert is DeserializeXmlNode(). This method takes JSON text and deserializes it into an XmlNode.
Example 1
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(); }
Output
{"person":{"@id":"1","name":"Alan","url":"http://www.google1.com","role":"Admin1"}}
Example 2
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(); }
Output
'?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' } ] }
- Related Articles
- How to convert XML to JSON array in Java?
- Difference between JSON and XML
- How to convert a bean to XML using JSON-lib API in Java?
- How to work with XML and JSON in .NET?
- Convert a JSON object to XML format in Java?\n
- How to convert a bean to XML without type hints using JSON-lib API in Java?
- How to Deserializing JSON to .NET object using Newtonsoft json in C# and pick only one value from the array?
- How to convert JSON text to JavaScript JSON object?
- How to convert JSON string to array of JSON objects using JavaScript?
- How to convert a Collection to JSON Array using JSON-lib API in Java?
- How to convert a Map to JSON object using JSON-lib API in Java?
- How to convert string to JSON using Python?
- How to convert an array to JSON Array using JSON-lib API in Java?\n
- How to convert the JSON object to a bean using JSON-lib API in Java?
- How to convert a JSON array to array using JSON-lib API in Java?\n

Advertisements