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. private string toField;
  8. private string fromField;
  9. /// <remarks/>
  10. public string to
  11. {
  12. get { return this.toField; }
  13. set { this.toField = value; }
  14. }
  15. /// <remarks/>
  16. public string from
  17. {
  18. get { return this.fromField; }
  19. set { this.fromField = value; }
  20. }
  21. }
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.