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

Using Music to Focus

Recently, Carl Frankin, software developer, musician and presenter on the popular programming podcast DotNet Rocks, released a music album aimed a software developers. The album is called Music to Code By and comprises of 3 x 25 minute long music tracks.

The idea behind the album is that each track is the length of one pomodoro and you listen to the music and focus on your work for 25 minutes and then take a break. After this break, you work for another 25 minutes. The music is designed help you concentrate and filter out any distractions to allow you to focus.

Carl Franklin - Music to Code By
Carl Franklin – Music to Code By

I purchased a copy of the album last week. It costs $20 dollars and I thought I would give it a try. For this price you get the CD, which should turn up at some point soon and a digital copy of the album. If I am honest I am not bothered about the physical disc. I will probably look at it and think “ohhh shiny” and then put it on a shelf. I haven’t bought a real CD for about 8 years. It would be good to see a digital only offering that is cheaper for some people, but it’s not a big deal.

I have tried the album out on a project last week. It wasn’t a coding project, but I needed to complete my 2nd e-book for Syncfusion. I used this album to help me focus and get the job completed. It took me 3 days to complete the project.

Did it work?

Yeah, the album definitely helped me focus and split my work down to 25 minute intervals. The music isn’t what I would normally listen too and I would describe it as a series smooth funk tracks, but to get the job done it certainly helped. I wouldn’t use this all the time as the tracks could get repetitive after a while and I normally like to listen to film soundtracks whilst I am working, but if I have a deadline looming and I really need to just focus, then I would use this album as I am not as attached to the music as I would be with my favourite film soundtrack. I think that’s a key point actually because I am not actually listening to the music I am just using it block out the environment around me, where as with a film soundtrack (and I like action scores) part of my brain is also listening to the music.

Coffitivity and Background Sounds

I was discussing this with a colleague of mine and he told be about a site that he uses called Coffitivity. This is a completely different twist using music or sound to focus with. What is presented here are streamed recordings of ambient cafe / coffee shop sounds. Imagine you are sitting in Starbucks doing some work whilst enjoying a coffee with all the noises of the cafe in the background. That is exactly what this is.

Coffitivity - Ambient cafe sounds
Coffitivity – Ambient cafe sounds

I am listening to a track on it as I write this post and I really like it. I am not thinking about the noise but it is helping me block out the surrounding work environment around me.

Which is Best?

I think it would be unfair to say whether Coffitivity is better than Music to Code By or the other way around. They are both different. I could see myself using coffitivity a lot more for general work as the ambient sounds are quite soothing, where as if I am up against it and I really need to get something done for a deadline, I can see myself using Music to Code By as my musical weapon of choice.

As with anything, this all subjective, what works for me wont necessarily work for you, but I would give both a try. Coffitivity is free to try out with a few tracks, so you should definitely give it ago. There is a premium version you can pay for to get access to more tracks. Music to Code By costs $20. Both are worth checking out.