Convert XML to Json C#.Net

How to convert XML to Json string using C#.Net by a simple tutorial with example. XDocument to parse XML and convert to List. Serialize XML to Json.
In today's article I will show you a simple tutorial with example how you can convert an XML data into Json using C#.net. In this tutorial we will take an XML string and convert it into list. After that we will convert the List into Json string using c#.net. This we simple code example we can use in the asp.net core mvc, web Api, console application and in windows application.

So, in this article we will use console application in .NET core using c#.net. First create a new console application and add a class file names as employee. Add the below code into the class file. 

 public class EmployeeModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string Email { get; set; }
} 
Above class properties we will use to map the xml tag values. Here one thing is most important you must define the class property same as per your XML structure. Now here is the XML string.
 string xmlString = "<Company>" +
    "<Employee><Id>1</Id><Name>Rakesh</Name><Address>Demo Address 1</Address><Email>r@rmail.com</Email></Employee>" +
    "<Employee><Id>2</Id><Name>Vinodh</Name><Address>Demo Address 2</Address><Email>v@rmail.com</Email></Employee>" +
    "<Employee><Id>3</Id><Name>Lakshmi</Name><Address>Demo Address 3</Address><Email>l@rmail.com</Email></Employee>" +
    "<Employee><Id>4</Id><Name>Tana</Name><Address>Demo Address 4</Address><Email>t@rmail.com</Email></Employee>" +
    "</Company>"; 
Please check the above XML string. This XML is holding the employee detail. We will convert this XML string into the Employee List object. Let's check the code.
 string xmlString = "<Company>" +
    "<Employee><Id>1</Id><Name>Rakesh</Name><Address>Demo Address 1</Address><Email>r@rmail.com</Email></Employee>" +
    "<Employee><Id>2</Id><Name>Vinodh</Name><Address>Demo Address 2</Address><Email>v@rmail.com</Email></Employee>" +
    "<Employee><Id>3</Id><Name>Lakshmi</Name><Address>Demo Address 3</Address><Email>l@rmail.com</Email></Employee>" +
    "<Employee><Id>4</Id><Name>Tana</Name><Address>Demo Address 4</Address><Email>t@rmail.com</Email></Employee>" +
    "</Company>";

XDocument doc = new XDocument();
if (!string.IsNullOrEmpty(xmlString))
{
    doc = XDocument.Parse(xmlString);
}
var empList = doc.Root.Elements("Employee").Select(e => new EmployeeModel
{
    Id = (int)e.Element("Id"),
    Name = (string)e.Element("Name"),
    Address = (string)e.Element("Address"),
    Email = (string)e.Element("Email"),
});
var jsonValue = JsonSerializer.Serialize(empList); 
In above first i have defined a string named as xmlString which is holding an XML. This will be our sample xml. After this we will create object for XDocument. Here XDocument is a class which hold the necessary information required for validating an XML document. 
XDocument doc = new XDocument();
if (!string.IsNullOrEmpty(xmlString))
{
    doc = XDocument.Parse(xmlString);
}
After creating the object, we will validate xml string for null of empty to avoid exception. To parse and XML we will use XDocument.Parse(xmlString). Now After parsing the XML we will convert the XML string into the List. 
var empList = doc.Root.Elements("Employee").Select(e => new EmployeeModel
{
    Id = (int)e.Element("Id"),
    Name = (string)e.Element("Name"),
    Address = (string)e.Element("Address"),
    Email = (string)e.Element("Email"),
});
In above code I have used LINQ to convert the XML into list. Here i have used Select to assign the value to each property. Now let's check the list. 

XML to List in C#.Net
Now to convert the list into Json string JsonSerializer.Serialize(empList) have been used, Refer the code mentioned below.
var jsonValue = JsonSerializer.Serialize(empList); 
Here is the Text Visualize view.

Json data
You can see we have successfully converted the XML into Json string using c#.net.

Convert XML to Json C# .Net

Post a Comment