Convert XML and JSON to C# Class

With the release of new version of the visual studio, developers are now getting more and more flexibility.I Microsoft made developer to only think logic and not to worry about the syntax and other manual works during any software development. That's why every 1-2 year we are getting new version of the Visual Studio and other IDE by Microsoft.

So today I am going to show you how simply you can achieve this using your visual studio. Just follow the below steps

  • Copy your xml/json data (note that it should be in a proper JSON or XML format otherwise dont blame me :) )
  • Open your visual studio (I have tested it in visual studio 2015 preview) and create a blank class file.
  • Go to Edit
  • Select Past Edit, Here you will get the both options 
    • Paste JSON as Classes
    • Paste XML as Classes
  • Choose any option as per the copied data in your clipboard.
  • Now your are done :)
So lets see this screen by screen... (not required for you developers but I am adding it here ..)
I have created a simple JSON data file with 3 properties .


 "User": { 
           "Name": "Dharmendra Kumar", 
           "Address": "Noida", 
           "Site": "http://www.dkumar.co.in" 
         } 
}
Above JSON data contains a class having three properties. Now lets create an empty class file in visual studio. see the below pic

Now Lets see how the above JSON will be converted to class file. Now I will go to File Menu and then Paste Special



When you will select the above shown option, you JSON data will be converted to the c# class, see the below image


So a default class has been created with name RootObject which have a property of type User and the User class is also created with all the properties.
You can try this with XML data also.

Note: I haven't seen Paste XML as Classes in Visual Studio 2012 & 2013 Professional Editions. There might be some configuration issue and you may be able to see it in yours. I have shown above examples in Visual Studio 2015 preview edition.

Comments

Post a Comment

Thanks for your valuable feedbacks.Keep visiting the blog...