TIP: “Paste XML as Classes” in Visual Studio 2012

pastespecial

In the past, when you had some XML document that you wanted to translate into classes we had to create / generate a schema based on the XML file. Next, we had to generate a class based on the schema with an external tool. Not all too user friendly and somewhat time consuming.

In Visual Studio 2012 you copy the XML you want to create a class/classes for, place the cursor in a class file on the location you want the code to be added and select the following menu items: Edit – Paste Special – Paste XML as Classes. And you’re done! Just like that…


If you want to try real quick, there’s a sample XML file (books.xml) available here.


EDIT
The feature is .NET Framework 4.5 specific. Taken from this MSDN article 'Generating Data Type Classes from XML':
".NET Framework 4.5 includes a new feature to generate data type classes from XML."

Comments

# re: TIP: “Paste XML as Classes” in Visual Studio 2012

Wednesday, October 24, 2012 4:51 PM by DaveShaw

What version of Visual Studio is required? I cannot for the life of me get this to work in VS2012 Premium, there is never a Paste Special, and even adding the "Paste XML as Classes" button the menu by hand does nothing when I click it.

# re: TIP: “Paste XML as Classes” in Visual Studio 2012

Thursday, October 25, 2012 8:36 AM by Rick van den Bosch

@DaveShaw,

The screenshot was taken from a VS2012 Premium installation. So that should be working.

The feature is part of .NET 4.5 (see msdn.microsoft.com/.../hh371548.aspx)

Hope this helps

# re: TIP: “Paste XML as Classes” in Visual Studio 2012

Friday, October 26, 2012 5:19 AM by Robert

This even works in VS Pro.

But you need to target .NET 4.5

BTW, there is a "Paste JSON as Classes" too!

# re: TIP: “Paste XML as Classes” in Visual Studio 2012

Monday, February 18, 2013 12:02 PM by Enzo Contini

Perhaps some of you can answer to the question I posted here: social.msdn.microsoft.com/.../b426d123-5100-4999-9cb0-094dbed87c60 related to how to make availabel that EDIT voice item related to “Paste XML as Classes” and eventually "Paste JSON as Classes" too.

Kind regards

# re: TIP: “Paste XML as Classes” in Visual Studio 2012

Monday, February 18, 2013 2:59 PM by Enzo Contini

I succeded to use that feature at the end! ;-)

That feature do not work if the project is a Windows Phone vers. 7.1 !!! It works if the target is Windows Phone 8.0

I think it's a bug of VS2012 ....

# re: TIP: “Paste XML as Classes” in Visual Studio 2012

Wednesday, February 20, 2013 1:43 PM by Rick van den Bosch

@Enzo Contini

If you think this is a bug for Visual Studio 2012, you could consider finding/posting it on connect.microsoft.com

(specifically  connect.microsoft.com/VisualStudio)

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Please add 5 and 4 and type the answer here: