C# XML Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
To convert XML to JSON in C#, you can use the JsonConvert
class from the Newtonsoft.Json library (also known as Json.NET). Here's an example:
Install the Newtonsoft.Json package:
In your project, you need to install the Newtonsoft.Json package. You can do this using the Package Manager Console or the NuGet Package Manager in Visual Studio.
Install-Package Newtonsoft.Json
Add the following using
statements:
using System.Xml; using Newtonsoft.Json;
public static string ConvertXmlToJson(string xml) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); string json = JsonConvert.SerializeXmlNode(xmlDoc); return json; }
In this method, you first load the XML string into an XmlDocument
object. Then, you use the JsonConvert.SerializeXmlNode()
method to convert the XmlDocument
to a JSON string.
ConvertXmlToJson()
method to convert an XML string to a JSON string:string xml = @"<Person> <Id>1</Id> <Name>John Doe</Name> <Age>30</Age> </Person>"; string json = ConvertXmlToJson(xml); Console.WriteLine(json);
In this example, you create an XML string and then call the ConvertXmlToJson()
method to convert it to a JSON string. The resulting JSON string is printed to the console.
Note that this example assumes you have a valid XML string. You may need to add error handling for invalid XML input.
C# convert XML to JSON example: A basic example of converting XML to JSON using Newtonsoft.Json.
using Newtonsoft.Json; using System; using System.Xml.Linq; class Program { static void Main() { string xmlString = "<person><name>John Doe</name><age>30</age></person>"; // Convert XML to JSON string jsonString = ConvertXmlToJson(xmlString); Console.WriteLine(jsonString); } static string ConvertXmlToJson(string xmlString) { XElement xml = XElement.Parse(xmlString); string json = JsonConvert.SerializeXNode(xml); return json; } }
XML to JSON conversion using Newtonsoft.Json in C#: Using Newtonsoft.Json to convert XML to JSON in C#.
using Newtonsoft.Json; using System; using System.Xml.Linq; class Program { static void Main() { string xmlString = "<book><title>C# Programming</title><author>John Smith</author></book>"; // Convert XML to JSON string jsonString = ConvertXmlToJson(xmlString); Console.WriteLine(jsonString); } static string ConvertXmlToJson(string xmlString) { XElement xml = XElement.Parse(xmlString); string json = JsonConvert.SerializeXNode(xml); return json; } }
Using Json.NET to convert XML to JSON in C#: Utilizing Json.NET (Newtonsoft.Json) for XML to JSON conversion.
using Newtonsoft.Json; using System; using System.Xml.Linq; class Program { static void Main() { string xmlString = "<employee><name>Alice</name><salary>50000</salary></employee>"; // Convert XML to JSON string jsonString = ConvertXmlToJson(xmlString); Console.WriteLine(jsonString); } static string ConvertXmlToJson(string xmlString) { XElement xml = XElement.Parse(xmlString); string json = JsonConvert.SerializeXNode(xml); return json; } }
C# deserialize XML to JObject and convert to JSON: Deserializing XML to JObject and converting it to JSON using Json.NET.
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Xml.Linq; class Program { static void Main() { string xmlString = "<product><name>Laptop</name><price>999.99</price></product>"; // Deserialize XML to JObject and convert to JSON string jsonString = ConvertXmlToJson(xmlString); Console.WriteLine(jsonString); } static string ConvertXmlToJson(string xmlString) { XElement xml = XElement.Parse(xmlString); JObject jsonObject = JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeXNode(xml)); return jsonObject.ToString(); } }
Transforming XML to JSON with C#: Transforming XML to JSON using XNode and Json.NET.
using Newtonsoft.Json; using System; using System.Xml.Linq; class Program { static void Main() { string xmlString = "<order><item>ProductA</item><quantity>2</quantity></order>"; // Transform XML to JSON string jsonString = TransformXmlToJson(xmlString); Console.WriteLine(jsonString); } static string TransformXmlToJson(string xmlString) { XElement xml = XElement.Parse(xmlString); string json = JsonConvert.SerializeXNode(xml, Formatting.Indented); return json; } }
XML to JSON conversion library in C#: Using a custom XML to JSON conversion library in C#.
using System; using System.Xml.Linq; class Program { static void Main() { string xmlString = "<animal><type>Lion</type><sound>Roar</sound></animal>"; // Convert XML to JSON string jsonString = XmlToJsonConverter.Convert(xmlString); Console.WriteLine(jsonString); } } static class XmlToJsonConverter { public static string Convert(string xmlString) { // Custom XML to JSON conversion logic // (Implementation not provided here for brevity) return "JSON representation of XML"; } }
C# convert XML attribute to JSON property: Handling XML attributes during XML to JSON conversion.
using Newtonsoft.Json; using System; using System.Xml.Linq; class Program { static void Main() { string xmlString = "<person age='25'><name>Alice</name></person>"; // Convert XML to JSON string jsonString = ConvertXmlToJson(xmlString); Console.WriteLine(jsonString); } static string ConvertXmlToJson(string xmlString) { XElement xml = XElement.Parse(xmlString); string json = JsonConvert.SerializeXNode(xml, Formatting.Indented, true); return json; } }
Converting nested XML elements to nested JSON in C#: Handling nested XML elements during XML to JSON conversion.
using Newtonsoft.Json; using System; using System.Xml.Linq; class Program { static void Main() { string xmlString = "<person><name>Bob</name><address><city>New York</city><zip>10001</zip></address></person>"; // Convert XML to JSON string jsonString = ConvertXmlToJson(xmlString); Console.WriteLine(jsonString); } static string ConvertXmlToJson(string xmlString) { XElement xml = XElement.Parse(xmlString); string json = JsonConvert.SerializeXNode(xml, Formatting.Indented, true); return json; } }