08 September 2014 Visual Studio 2013, XML, JSON Robert Muehsig

I was looking for an easy way to “convert” an XML structure to a normal C# structure and stumbled upon a handy feature of Visual Studio 2013. This also works for any JSON content as well.

What you need:

Copy your XML or JSON in the clipboard and make sure you have a .NET 4.5 project selected. I’m not sure why this limitation is in place, but you can use the generated classes in .NET 4.0 and even lower I guess.

Then just click Edit - Paste Special - Paste XML/JSON As Classes.


The generated classes:

The generated classes should work with JSON.NET or any XML Serializer. The Code for the XML is a bit messy, but works and gives you a starting point.

Sample from here:

public class Rootobject
    public string firstName { get; set; }
    public string lastName { get; set; }
    public bool isAlive { get; set; }
    public int age { get; set; }
    public float height_cm { get; set; }
    public Address address { get; set; }
    public Phonenumber[] phoneNumbers { get; set; }
    public object[] children { get; set; }
    public object spouse { get; set; }

public class Address
    public string streetAddress { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string postalCode { get; set; }

public class Phonenumber
    public string type { get; set; }
    public string number { get; set; }

Note: There are many other web based XML/JSON to C# converter out there - so feel free to use whatever works for you. I didn’t know this little feature.

Written by Robert Muehsig

Software Developer - from Saxony, Germany - working on primedocs.io. Microsoft MVP & Web Geek.
Other Projects: KnowYourStack.com | ExpensiveMeeting | EinKofferVollerReisen.de