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.
- <address>
- <to>SKN</to>
- <from>LKO</from>
- </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
- [System.SerializableAttribute()]
- [System.ComponentModel.DesignerCategoryAttribute("code")]
- [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
- [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
- public partial class address
- {
-
- private string toField;
-
- private string fromField;
-
- /// <remarks/>
- public string to
- {
- get { return this.toField; }
- set { this.toField = value; }
- }
-
- /// <remarks/>
- public string from
- {
- get { return this.fromField; }
- set { this.fromField = value; }
- }
- }
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.