Visual Studio tip: easily generate C# classes from either JSON or XML data

Posted: (EET/GMT+2)

 

Processing JSON and XML based data is a very common need, especially if you are integrating two systems together. When working with C#, you usually want to work with this data using strongly-typed classes and their properties.

However, converting either XML or JSON data into C# classes is tedious and slow work if done by hand. Luckily, Visual Studio brings a helping hand exactly here, but you need to know where to look.

To quickly generate C# classes from either XML or JSON data, do the following. First, make sure your XML or JSON data is syntactically correct and contains a representative amount of data. Then, copy this data onto the clipboard.

Then, open up a C# class file in Visual Studio, and go to the Edit menu. There, choose the Paste Special submenu command, and from there, select either "Paste XML As Classes" or "Paste JSON As Classes". Once done, you will see ready-made C# classes in front of you!

Here are additional details about the functionality.

Hope this helps!