How To Generate C# class from XML?

Using Visual Studio 2012 or newer you can just ‘Special Paste’ your XML file as C# classes.

Three Simple Steps

  1. Copy your XML file’s content to the clipboard
  2. In the editor, select the place where you want your classes to be pasted
  3. From the menu, select EDIT > Paste Special > Paste XML As Classes

Video and Illustration

Generate C# Class from XML using Special Paste Visual Studio

Visual Studio – Special Paste

Examples from the Video

<?xml version="1.0" encoding="utf-8"?>
<friends xmlns="http://schemas.microsoft.com/office/outlook/2010/06/socialprovider.xsd">
  <person>
    <userID>7647</userID>
    <firstName>John</firstName>
    <lastName>Doe</lastName>
    <company>My Test Company</company>
    <title>Product Manager</title>
    <anniversary>2005-05-17</anniversary>
    <birthday>1979-08-09</birthday>
    <emailAddress>john.doe@MyTestCompany.com</emailAddress>
    <emailAddress2>john.doe@test.com</emailAddress2>
    <phone>800-555-1212</phone>
    <cell>888-555-1212</cell>
    <workPhone>425-555-1212</workPhone>
    <creationTime>2022-01-08T08:30:20-07:00</creationTime>
    <expirationTime>2022-01-09T11:40:14-07:00</expirationTime>
    <gender>female</gender>
  </person>
  <person>
    <userID>5012</userID>
    <firstName>Mikle</firstName>
    <lastName>Affronti</lastName>
    <fileAs>Affronti, Michael (Contoso Ltd.)</fileAs>
    <company>My Test Company 2</company>
    <title>Sales Director</title>
    <anniversary>2009-06-21</anniversary>
    <birthday>1980-09-10</birthday>
    <emailAddress>michael@contoso.com</emailAddress>
    <emailAddress2>michael@fabrikam.com</emailAddress2>
    <emailAddress3>michael@adventureworks.com</emailAddress3>
    <phone>800-555-1212</phone>
    <cell>888-555-1212</cell>
    <workPhone>425-555-1212</workPhone>
    <creationTime>2010-01-08T08:30:20-08:00</creationTime>
    <expirationTime>2010-01-09T11:40:14-08:00</expirationTime>
    <gender>male</gender>
  </person>
</friends>

 

Leave a Reply

Your email address will not be published.