Quickly create json & XML classes in Visual Studio

I've done a lot of integrations with api's in my time. I'm always amazed when I speak to other developers that many don't know about this nifty little feature in Visual Studio. It has been in there for as long as I can remember and will save you lots of time.

The Problem

Every time you need to call out to api's in .net, the best way is to map it to a POCO class. To do this, you generally have to take the (often badly) written api documentation and set about creating the required classes. This is quite a labor intensive task.

The Solution

Good old 'Paste Special'.

All you need to do is copy the sample XML/json in to your clipboard, then selected what you want to past in. Visual Studio will then auto-magically create the required classes for you. For example, given the following json:

[
	{ 
		firstName : 'John',
		lastName : 'Clee',
		rating : 10,
		dateStarted : '2016-09-22T09:00:00'
	},
	{ 
		firstName : 'Bob',
		lastName : 'Builder',
		rating : 3,
		dateStarted : '2016-09-21T16:00:00'
	}
]

It will generate the folowing c# POCO's:

public class Rootobject
{
    public Class1[] Property1 { get; set; }
}

public class Class1
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public int rating { get; set; }
    public DateTime dateStarted { get; set; }
}

You can then go and change the name of the root object and class to match something more sensible. As well as probably change the array to an IEnumerable<> too while you are there.

Again with the following XML:


	
		John
		Clee
		10
		2016-09-22T09:00:00
	
	
		Bob
		Builder
		3
		2016-09-21T16:00:00
	

It will create this:

/// 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class people
{

    private peoplePerson[] personField;

    /// 
    [System.Xml.Serialization.XmlElementAttribute("person")]
    public peoplePerson[] person
    {
        get
        {
            return this.personField;
        }
        set
        {
            this.personField = value;
        }
    }
}

/// 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class peoplePerson
{

    private string firstNameField;

    private string lastNameField;

    private byte ratingField;

    private System.DateTime dateStartedField;

    /// 
    public string firstName
    {
        get
        {
            return this.firstNameField;
        }
        set
        {
            this.firstNameField = value;
        }
    }

    /// 
    public string lastName
    {
        get
        {
            return this.lastNameField;
        }
        set
        {
            this.lastNameField = value;
        }
    }

    /// 
    public byte rating
    {
        get
        {
            return this.ratingField;
        }
        set
        {
            this.ratingField = value;
        }
    }

    /// 
    public System.DateTime dateStarted
    {
        get
        {
            return this.dateStartedField;
        }
        set
        {
            this.dateStartedField = value;
        }
    }
}

For some reason, with the XML paste, it doesn't create auto properties. After a few Alt+Enter's with Resharper we can cut that down to:

[XmlType(AnonymousType = true)]
[XmlRootAttribute(Namespace = "", IsNullable = false)]
public class people
{
    [XmlElementAttribute("person")]
    public peoplePerson[] person { get; set; }
}

[XmlTypeAttribute(AnonymousType = true)]
public class peoplePerson
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public byte rating { get; set; }
    public DateTime dateStarted { get; set; }
}

If you use Resharper (and why wouldn't you?!?) it will want to change the field names. This is to match the naming convention rules you have set up. Now that task has taken you a lot less time than your project manager had allocated, I'd suggest you go and have a coffee....