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:
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"?>
|
1 |
<span class="lnum"> 100: </span> <span class="kwrd">return</span> <span class="kwrd">this</span>.publish_dateField; |
3: <book id="bk102">[crayon-519ae793d9a39/]
5: <title>Midnight Rain</title>[crayon-519ae793d9a39/]
7: <price>5.95</price>[crayon-519ae793d9a39/]
9: <description>A former architect battles corporate zombies,[crayon-519ae793d9a39/]
11: of the world.</description>[crayon-519ae793d9a39/]
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/>[crayon-519ae793d9a39/]
3: [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)][crayon-519ae793d9a39/]
5: {
[crayon-519ae793d9a39/]
7: private catalogBook bookField;[crayon-519ae793d9a39/]
9: /// <remarks/>[crayon-519ae793d9a39/]
11: {
[crayon-519ae793d9a39/]
13: {
[crayon-519ae793d9a39/]
15: }
[crayon-519ae793d9a39/]
17: {
[crayon-519ae793d9a39/]
19: }
[crayon-519ae793d9a39/]
21: }
[crayon-519ae793d9a39/]
23: /// <remarks/>[crayon-519ae793d9a39/]
25: public partial class catalogBook[crayon-519ae793d9a39/]
27:
[crayon-519ae793d9a39/]
29:
[crayon-519ae793d9a39/]
31:
[crayon-519ae793d9a39/]
33:
[crayon-519ae793d9a39/]
35:
[crayon-519ae793d9a39/]
37:
[crayon-519ae793d9a39/]
39:
[crayon-519ae793d9a39/]
41:
[crayon-519ae793d9a39/]
43: public string author[crayon-519ae793d9a39/]
45: get
[crayon-519ae793d9a39/]
47: return this.authorField;[crayon-519ae793d9a39/]
49: set
[crayon-519ae793d9a39/]
51: this.authorField = value;[crayon-519ae793d9a39/]
53: }
[crayon-519ae793d9a39/]
55: /// <remarks/>[crayon-519ae793d9a39/]
57: {
[crayon-519ae793d9a39/]
59: {
[crayon-519ae793d9a39/]
61: }
[crayon-519ae793d9a39/]
63: {
[crayon-519ae793d9a39/]
65: }
[crayon-519ae793d9a39/]
67:
[crayon-519ae793d9a39/]
69: public string genre[crayon-519ae793d9a39/]
71: get
[crayon-519ae793d9a39/]
73: return this.genreField;[crayon-519ae793d9a39/]
75: set
[crayon-519ae793d9a39/]
77: this.genreField = value;[crayon-519ae793d9a39/]
79: }
[crayon-519ae793d9a39/]
81: /// <remarks/>[crayon-519ae793d9a39/]
83: {
[crayon-519ae793d9a39/]
85: {
[crayon-519ae793d9a39/]
87: }
[crayon-519ae793d9a39/]
89: {
[crayon-519ae793d9a39/]
91: }
[crayon-519ae793d9a39/]
93:
[crayon-519ae793d9a39/]
95: [System.Xml.Serialization.XmlElementAttribute(DataType = "date")][crayon-519ae793d9a39/]
97: {
[crayon-519ae793d9a39/]
99: {
[crayon-519ae793d9a39/]
101: }
|
1 |
<span class="lnum"> 134: </span> } |
103: {
[crayon-519ae793d9e21/]
105: }
[crayon-519ae793d9e21/]
107:
[crayon-519ae793d9e21/]
109: public string description[crayon-519ae793d9e21/]
111: get
[crayon-519ae793d9e21/]
113: return this.descriptionField;[crayon-519ae793d9e21/]
115: set
[crayon-519ae793d9e21/]
117: this.descriptionField = value;[crayon-519ae793d9e21/]
119: }
[crayon-519ae793d9e21/]
121: /// <remarks/>[crayon-519ae793d9e21/]
123: public string id[crayon-519ae793d9e21/]
125: get
[crayon-519ae793d9e21/]
127: return this.idField;[crayon-519ae793d9e21/]
129: set
[crayon-519ae793d9e21/]
131: this.idField = value;[crayon-519ae793d9e21/]
133: }
[crayon-519ae793d9e21/]
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.


Comments (1)
Tony -
April 2, 2012 at 10:31 am
Yes, and it's one of my favourite little features. Always nice finding cute things like that!