Generating Data Type Classes from XML

I was shown a nice trick recently in Visual Studio that I never realised existed. The technique is an easy way to create a C# class from an XML or JSON document. Have you ever been in the situation where you are taking an XML or JSON feed from one system and need to integrated it into a project you are working on? I have plenty of times and creating the data type from that XML so you can de-serialize it back can be a pain.

Generating Data Type Classes from XML
Generating Data Type Classes from XML

At first I thought I must be the only person who doesn’t know this, but on discussing it with some team members, they hadn’t hear of it either. Lets take a look at an example with XML. I have taken an example excerpt of XML from the W3CSchools website.

<breakfast_menu>
    <food>
        <name>Belgian Waffles</name>
        <price>$5.95</price>
        <description>
            Two of our famous Belgian Waffles with plenty of real maple syrup
        </description>
        <calories>650</calories>
    </food>
    <food>
        <name>Strawberry Belgian Waffles</name>
        <price>$7.95</price>
        <description>
            Light Belgian waffles covered with strawberries and whipped cream
        </description>
        <calories>900</calories>
    </food>
    <food>
        <name>Berry-Berry Belgian Waffles</name>
        <price>$8.95</price>
        <description>
            Light Belgian waffles covered with an assortment of fresh berries and whipped cream
        </description>
        <calories>900</calories>
    </food>
    <food>
        <name>French Toast</name>
        <price>$4.50</price>
        <description>
            Thick slices made from our homemade sourdough bread
        </description>
        <calories>600</calories>
    </food>
    <food>
        <name>Homestyle Breakfast</name>
        <price>$6.95</price>
        <description>
            Two eggs, bacon or sausage, toast, and our ever-popular hash browns
        </description>
        <calories>950</calories>
    </food>
</breakfast_menu>

If you take the above XML and place it into the clip board. Then create or open an existing visual studio project. Goto the Edit Menu and select “Paste Special” and then “Paste XML as Classes“. This will create a set of classes that represents the XML data structure.

Advertisements

Visual Studio 2013 New IDE Features

Disclaimer : A reader over at Reddit commented that I didn’t mention which edition of Visual Studio I was basing this article on as some of the features are not available on certain editions of the tools. This article is based on Visual Studio 2013 Ultimate Edition which I use via my companies MSDN subscription.

Now that Visual Studio 2013 has been released and I have had a little play around with it, I thought I would write about some of my favorite new IDE features. Microsoft has added in some useful features that will really make using the IDE a richer experience.

As a larger package Microsoft has packed a lot into Visual Studio 2013. For a more complete look at the new features in this development environment you can view the official release notes at MSDN.

Visual Studio 2013 Integrated Development Environment
Visual Studio 2013 Integrated Development Environment

The new IDE features I want to cover are:

    • Code Preview Scroll Bar
    • Peek Definition
    • Code Lenses
    • Test Explorer – Sort by Class
    • Sign in to Visual Studio

    Softening the Blow of the Visual Studio 2012 User Interface

    I really like Microsoft’s new version of Visual Studio. I even like where they are going with the user interface, but out of the box, I don’t think it is perfect. I really don’t like the SHOUTY uppercase menus, and whilst I don’t mind the default colour theme too much, it isn’t great for staring at on a long coding session.

    In this post, I will cover 2 very easy tweaks that you can do to Visual Studio 2012 to make the user interface much better (in my opinion of course). I have recently been getting my team to update parts of our code base to the new tool set  and most people had the same feelings about the user interface as me, but most of the team have now done these tweaks.

    Turn Off Upper Case Menus

    Visual Studio Upper Case Menus
    Visual Studio Upper Case Menus

    I don’t know why Microsoft decided to go with the Upper Case menus, but it is really easy to disable them. Just follow these basic steps.