There is a new function since Visual Studio 2012, which means, that it is also available in the recently released Visual Studio 2013. This pretty tiny, but really cool feature allows you to generate C# classes based on XML or JSON input data.

I am not only going to show you where the function hides, I will also show you an example to make sure you realize the power behind the functionality. Well, to be honest, the only thing you’ll gain is time. But isn’t it always about efficiency?! Yes it is, there we go with the example:

First of all we need some JSON or XML. I decided to take the following JSON as an example input:

{
	"library": {
		"books": {
			"author": {
				"-name": "Jon Skeet",
				"book": [
					{ "title": "C# in Depth, Third Edition" },
					{ "title": "C# in Depth, Second Edition" },
					{ "title": "C# in Depth" }
				]
			}
		}
	}
}

Let’s generated a bunch of C# classes from this JSON. It is really simple if you know where the particular function hides within the menus of Visual Studio. Go to EDIT, Paste Special, Paste JSONĀ as the following screenshot demonstrates:

Paste Special MenuWhat do we get? We get a full object graph, model or whatever you want to call it. As above, I will show you the result to make clear how it looks like:

public class Rootobject
{
    public Library library { get; set; }
}

public class Library
{
    public Books books { get; set; }
}

public class Books
{
    public Author author { get; set; }
}

public class Author
{
    public string name { get; set; }
    public Book[] book { get; set; }
}

public class Book
{
    public string title { get; set; }
}

What do you think about this feature? Do you think this is awesome?! I do, because if you have a large file that you need to access through a C# program, you’ll save much time. I hope this feature will save you at least the time you needed reading this blog post.

Share this

Claudio Bernasconi

Claudio Bernasconi is a professional software developer, Microsoft certified technology specialist, blogger, technology enthusiast and founder of CarParking Schweiz. Since 2010 he works for KMS AG in Lucerne, Switzerland.