Convert XML/ JSON File To C# Class

In this blog, I am going to explain how to convert XML/JSON file to C# class.
 
Recently, I faced the problem that I had a big file in JSON format but I needed it in C# class. So, I found a quick solution in Visual Studio.

This is very easy and requires only 3 steps to do it.
 
First of all, it requires Visual Studio 2012+.
 
The 3 steps are as follows.
 
Step 1

Copy the XML file data.
  1. <address>  
  2.    <to>SKN</to>  
  3.    <from>LKO</from>  
  4. </address>  
Step 2

Add new, empty class file and put your XML data here and copy this data.

 
Step 3

Open that file and in menu, go to  Edit >> Paste Special >> Paste XML As Classes.
 
 
 
Result
  1. [System.SerializableAttribute()]  
  2. [System.ComponentModel.DesignerCategoryAttribute("code")]  
  3. [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]  
  4. [System.Xml.Serialization.XmlRootAttribute(Namespace = ""IsNullable = false)]  
  5. public partial class address  
  6. {  
  7.   
  8.     private string toField;  
  9.   
  10.     private string fromField;  
  11.   
  12.     /// <remarks/>  
  13.     public string to  
  14.     {  
  15.         get { return this.toField; }  
  16.         set { this.toField = value; }  
  17.     }  
  18.   
  19.     /// <remarks/>  
  20.     public string from  
  21.     {  
  22.         get { return this.fromField; }  
  23.         set { this.fromField = value; }  
  24.     }  
  25. }  
 
 
Note

While choosing the Edit > Paste Special menu, ensure that the Visual Studio venture that your class record is under, has its 'Objective Framework' set to .NET Framework 3.5+ for 'Glue JSON as Classes' and .NET Framework 4.5+ for 'Glue XML as Classes'; otherwise, these choices don't show up.
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now