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

By rickvdbosch
October 24, 2012


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.

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: 1

  1. Manfred Kipfelsberger says:

    this feature works well for me in a VS 2013 Class library project targeting .Net 4.5, but I also had issues with a Vsto project, where the menu entry didn’t show up.
    Also, when pasting in the empty class library project, it could not resolve system.xml.serialization, even though references and usings were set correctly. So this still has some bugs which need to be fixed.

    Nice to note btw that child nodes are transformed to named member classes where xsd generates only an items-array to access child nodes.

please leave a Reply to join the discussion...

  • Meta