Sometimes when integrating between external systems or reading XML from I/O, you might need to deserialize that XML string into a runtime object. Because converting an XML into an object will give lots benefits rather than parsing the XML and then extracting/manipulating nodes.

Luckily, in Visual Studio 2013 there is a new feature that can create a class with its properties on behalf of you according to the passed XML string. So rather than manually writing a class with lots of properties to map your XML structure, Visual Studio 2013 does it for you.

Here is a simple XML sample:

<?xml version="1.0" encoding="UTF-8"?>
<CATALOG>
  <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
    <Genre>
      <Chillout/>
      <Easy/>
      <Rock/>
    </Genre>
  </CD>
  <CD>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>CBS Records</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1988</YEAR>
    <Genre>
      <Chillout/>
      <Classic/>
      <Post-Rock/>
    </Genre>
  </CD>
  <CD>
    <TITLE>Greatest Hits</TITLE>
    <ARTIST>Dolly Parton</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>RCA</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1982</YEAR>
    <Genre>
      <Electronic/>
      <Trance/>
      <Vocal/>
    </Genre>
  </CD>
</CATALOG>

Open a new instance of Visual Studio 2013, create a new C# Project select Console Application.

Change the Target framework to .Net 4.5, this is really important, because this feature only works .Net 4.5 projects.

Add a new Class to the project by right-click the project and go to Add and then select Class.

Keep the existing class name because it will automatically change as we will see now.

Completely remove this code from the Class file and place your cursor between the namespace curly braces like this:

Now from the main menu, go to Edit > Paste Special > Click Paste XML as Class.

And Voila, you have a C# class that represents your pasted XML string. Now you can deserialize your XML strings into memory using an instance of your class.

This feature also applies for JSON and it works if you have Visual Studio 2012 Update 2.