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
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.
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 .
Above JSON data contains a class having three properties. Now lets create an empty class file in visual studio. see the below picI have created a simple JSON data file with 3 properties .
{
"User": {
"Name": "Dharmendra Kumar",
"Address": "Noida",
"Site": "http://www.dkumar.co.in"
}
}
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.
Nice and informative article...Embedded Training Institute in Chennai | Embedded Project Center in Chennai | Embedded Training in Chennai
ReplyDeleteVery useful tips.....
ReplyDeleteData | Informatica Training in Chennai | IBM Infosphere DataStage Training in Chennai
Nice post. Very interesting to read. Thank you for Sharing.
ReplyDeleteEmbedded System Training in Chennai | Embedded System Course in Chennai | No.1 Embedded Training Institute in Chennai | Online Embedded Training in Chennai
I gathered more information from your post..It's really amazing post..keep sharing..
ReplyDeleteNo.1 IOS Training Institute in Velachery | Best Android Training Institute in Velachery | Core Java Training Institute in Chennai