C# Program to Convert XML to JSON Objects File

You are currently viewing C# Program to Convert XML to JSON Objects File

Here, You are going to convert the objects file from XML to JSON format using the C# program. Yes, I’m going to explain the topic about “C# Program to Convert XML to JSON Objects File” here. Let’s see the conversion process below.

C# Program to Convert XML to JSON Objects File

Basically, You need to create an empty file for this conversion process. Later, You should copy and paste the following code in the empty file and save it with the name of “xml2json.cs”.

xml2json.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Newtonsoft.Json;

namespace JSonConverter
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml = "<Test><Name>Test class</Name><X>100</X><Y>200</Y></Test>";

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);

            Console.WriteLine("XML -> JSON: {0}", json);
            Console.ReadLine();
            
        }
    }
}
Read Also : Convert XML to CSV in Python | Python Tutorial

Conclusion

I hope this article helps you to convert XML to JSON objects file using C#. If you have any doubts regarding this topic please ask us via the comment section. We will help you. Share this article with other C# developers to help them.

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply