Skip to content
tucaz edited this page May 3, 2011 · 4 revisions

XmlToObjectParser is a simple dynamic library implemented in C# 4.0 to deal with XML structure.

Getting It

In order to get start you need to download the zip file containing the latest version of XmlToObjectParser.dll or compile yourself from the source code and add it as a reference in your project.

How it Works

Assume the following XML sample:

_sampleXml = @"<?xml version=""1.0"" encoding=""ISO-8859-1""?>
                <catalog>
                  <cd country=""USA"">
                    <title>Empire Burlesque</title>
                    <artist>Bob Dylan</artist>
                    <price>10.90</price>
                  </cd>
                  <cd country=""UK"">
                    <title>Hide your heart</title>
                    <artist>Bonnie Tyler</artist>
                    <price>10.0</price>
                  </cd>
                  <cd country=""USA"">
                    <title>Greatest Hits</title>
                    <artist>Dolly Parton</artist>
                    <price>9.90</price>
                  </cd>
                </catalog>";

The idea behind this is to able to read with XML as if it were a simple POCO instead of a confusing text structure. I know now it's possible to do something like this:

    var catalog = new XmlDocument();
    catalog.LoadXml(_sampleXml);

    var numberOfCDsinCatalog = catalog["catalog"].ChildNodes.Count; //Three 
    var titleFromUKCD = catalog.SelectSingleNode("/catalog/cd[2]/title").InnerText; //"Hide your heart"

But using an object approach the code is way more readable and far more extensible:

    var catalog = XmlToObjectParser.ParseFromXml(_sampleXml);

    var numberOfCDsinCatalog = catalog.catalog.cd.Count; //Same Three
    var titleFromUKCD = catalog.catalog.cd[1].title; // Same Hide your heart"

We can also have more readable queries:

    var allCDsFromUSA = (Predicate<dynamic>)((dynamic x) => x.country == "USA");
    var CDsFound = catalog.catalog.cd.FindAll(allCDsFromUSA);

Besides the fact that we can take advantage of dynamic types to add whatever behavior we want into our objects.

General Conventions

Object structure

By default the resulting object will be a pretty good copy of the XML structure including the way collections are handled this XML:

    <root>
        <orders>
            <order id="01">
                <customer>John</customer>
                ...
            </order>
            <order id="02">
                <customer>Andrea</customer>
                ...
            </order>
        </orders>
    </root>

will be traversed like a regular XML would with orders property not being a collection, but order instead:

    var firstOrder = xmlObj.root.orders.order[0] //first order object equivalent of xmlDoc["/root/orders/order[1]"]

Nodes and Attributes

The general behavior of the object structure when dealing with Nodes and Attributes are the same as XML so the id attribute of the order node in the xml sample above will be treated as a simple member in .NET object after the conversion.

However, nodes with no attributes or children will be treated as member also so the customer node would be accessed as

    var firstOrderCustomer = xmlObj.root.orders.order[0].customer //equivalent to xmlDoc["/root/orders/order[1]/customer"].InnerText

since it does not make sense to have anything different.

Thanks

Special thanks for @MemphisBR for reviewing and testing this