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.

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class breakfast_menu
{
    private breakfast_menuFood[] foodField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("food")]
    public breakfast_menuFood[] food
    {
        get
        {
            return this.foodField;
        }
        set
        {
            this.foodField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class breakfast_menuFood
{
    private string nameField;
    private string priceField;
    private string descriptionField;
    private ushort caloriesField;
    private string testAttributeField;

    /// <remarks/>
    public string name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }

    /// <remarks/>
    public string price
    {
        get
        {
            return this.priceField;
        }
        set
        {
            this.priceField = value;
        }
    }

    /// <remarks/>
    public string description
    {
        get
        {
            return this.descriptionField;
        }
        set
        {
            this.descriptionField = value;
        }
    }

    /// <remarks/>
    public ushort calories
    {
        get
        {
            return this.caloriesField;
        }
        set
        {
            this.caloriesField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string testAttribute
    {
        get
        {
            return this.testAttributeField;
        }
        set
        {
            this.testAttributeField = value;
        }
    }
}

It’s that easy. This also works with JSON documents, so this makes working with REST API’s really straight forward.

One thought on “Generating Data Type Classes from XML

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s