651.288.7000 info@intertech.com

Looking at beta software is always exciting.  It is as close as a grown adult techie can get to being the proverbial kid in a candy store.

Like many of you, I have been playing around with the latest beta for Visual Studio 11, and I happened to notice a very nice feature located under the IDE?s Edit menu, specifically Paste Special | XML As Classes:


image

First, be aware that this menu option on appears under the Edit menu if the active document in the IDE?s editor is indeed a code file.

As the name implies, this option will paste a blurb of valid XML on your clipboard into a set of C# classes (very useful). To illustrate, assume you have copied the following XML document to your clipboard:

1: <?xml version=”1.0″?>
2: <catalog>
3: <book id=”bk102″>
4: <author>Ralls, Kim</author>
5: <title>Midnight Rain</title>
6: <genre>Fantasy</genre>
7: <price>5.95</price>
8: <publish_date>2000-12-16</publish_date>
9: <description>A former architect battles corporate zombies,
10: an evil sorceress, and her own childhood to become queen
11: of the world.</description>
12: </book>
13: </catalog>

Now, once you have a C# code file opened in the IDE, select the menu option under consideration. You will find the following:

       1:  /// <remarks/>
       2:      [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
       3:      [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
       4:      public partial class catalog
       5:      {
       6:   
       7:          private catalogBook bookField;
       8:   
       9:          /// <remarks/>
      10:          public catalogBook book
      11:          {
      12:              get
      13:              {
      14:                  return this.bookField;
      15:              }
      16:              set
      17:              {
      18:                  this.bookField = value;
      19:              }
      20:          }
      21:      }
      22:   
      23:      /// <remarks/>
      24:      [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
      25:      public partial class catalogBook
      26:      {
      27:   
      28:          private string authorField;
      29:   
      30:          private string titleField;
      31:   
      32:          private string genreField;
      33:   
      34:          private decimal priceField;
      35:   
      36:          private System.DateTime publish_dateField;
      37:   
      38:          private string descriptionField;
      39:   
      40:          private string idField;
      41:   
      42:          /// <remarks/>
      43:          public string author
      44:          {
      45:              get
      46:              {
      47:                  return this.authorField;
      48:              }
      49:              set
      50:              {
      51:                  this.authorField = value;
      52:              }
      53:          }
      54:   
      55:          /// <remarks/>
      56:          public string title
      57:          {
      58:              get
      59:              {
      60:                  return this.titleField;
      61:              }
      62:              set
      63:              {
      64:                  this.titleField = value;
      65:              }
      66:          }
      67:   
      68:          /// <remarks/>
      69:          public string genre
      70:          {
      71:              get
      72:              {
      73:                  return this.genreField;
      74:              }
      75:              set
      76:              {
      77:                  this.genreField = value;
      78:              }
      79:          }
      80:   
      81:          /// <remarks/>
      82:          public decimal price
      83:          {
      84:              get
      85:              {
      86:                  return this.priceField;
      87:              }
      88:              set
      89:              {
      90:                  this.priceField = value;
      91:              }
      92:          }
      93:   
      94:          /// <remarks/>
      95:          [System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
      96:          public System.DateTime publish_date
      97:          {
      98:              get
      99:              {
     100:                  return this.publish_dateField;
     101:              }
     102:              set
     103:              {
     104:                  this.publish_dateField = value;
     105:              }
     106:          }
     107:   
     108:          /// <remarks/>
     109:          public string description
     110:          {
     111:              get
     112:              {
     113:                  return this.descriptionField;
     114:              }
     115:              set
     116:              {
     117:                  this.descriptionField = value;
     118:              }
     119:          }
     120:   
     121:          /// <remarks/>
     122:          [System.Xml.Serialization.XmlAttributeAttribute()]
     123:          public string id
     124:          {
     125:              get
     126:              {
     127:                  return this.idField;
     128:              }
     129:              set
     130:              {
     131:                  this.idField = value;
     132:              }
     133:          }
     134:      }
     135:   

 

As you can see, XML elements and attributes translate to classes and properties. While you might not like the exact manner in which code is generated, this integration is a useful starting point to build out an XML-based object model without dropping down to command line tools.

Happy exploring.

Like What You've Read?

Subscribe to the Blog.

Every Friday we send that week's content from our Developers via email. Try it out!

Some ad blockers can block the form below.

You have Successfully Subscribed!

Pin It on Pinterest

Share This