Friday, March 15, 2013

Tips & Tricks - Pasting XML as Classes

In this post we are going to discuss a simple yet very useful feature of Visual Studio 2012. If you have been working with XML then you might have felt the need to create concrete class types to hold the data from XML. Visual Studio 2012 comes with a feature which makes this work easier. We can easily create concrete class types from the XML data in the clipboard. It is one of the feature we can select to paste. I have used the customized paste with some other development tools including PL/SQL developer from AllroundAutomations which even allows users to create their own paste formats.

As discussed the feature allows to create classes from XML data in the clipboard. If the XML data is nested it would be creating separate classes to hold the nested data. There are conventions it follows for naming the class members based on the XML element's name.

Now copy some XML data. For our example we can use the following:

This xml has a Document Node, Student. It has two leaf level nodes including Id and Gender. It has a child node Name which has two further child elements including FirstName and LastName. Based on the convention, it creates leaf nodes as separate properties in the class for the nodes. For non-leaf nodes, it creates separate class types. Now copy the xml. So now we have this in the clipboard. Go to Visual Studio 2012 when a class file is already opened and select Edit -> Paste Special -> Paste XML as Classes. This would create the following classes.

As you have seen it has create separate properties for Id and Gender elements. It creates a separate class for StudentName corresponding to the Name element in the xml. The name of this extra types is based on the convention including the all parents and child elements in the nested hierarchy. The StudentName has further two properties FirstName and LastName corresponding to the child elements in the source XML. It has been used for a property in Student type. The data type for these elements are inferred so you can see Id has been created as a byte type. You can change it if you want.

The types wouldn't be created in a namespace so you might want to paste them directly or move them to your namespace section. If there is no XML in the clipboard then the menu item is just greyed out.

If the data in the clipboard is not in the xml format then the behavior doesn't seem to be very consistent. It is intially available if you have just copied some data, e.g. an image, it shows an error message when you select it. This could definitely improve.

Then it is greyed out eventually as I would generally expect. But I was able to reproduce this only a few times. This is not tied to the Cycle Clipboard Ring. We can use Clipboard ring to get to the items in the clipboard by cycling through these items using CTRL + SHIFT + V. But that would be a regular paste.

Feature Restrictions:
The feature is only available for class files added in a project opened in Visual Studio 2012. If you open a file separately in Visual Studio, the feature is is not available.

It would have been cooler if the option was available for the context menu as well. There is no support in the current version.

Other developers have noticed this feature is not available for WP7 projects. You can find a related discussion here:

No comments: