Generic XML Deserialization into Undefined Objects

Adenverd picture Adenverd · Nov 13, 2012 · Viewed 6.9k times · Source

I have a very long, very varied XML file that I am attempting to store portions of into a database. Now, I do not want to go through and hand-write 10,000 different objects to store the deserialized data into. Is there any way to define an Object based on what is found in the XML file?

For instance, if I had:

<objecttype1>
    <attr1>Some string of text</attr1>
</objecttype1>
<objecttype1>
    <attr2>123456789</attr2>
</objecttype1>

I would want an object similar to the following to be defined:

public class objecttype1 {
    public string attr1 {get; set;}
    public string attr2 {get; set;}
}

Basically, I want to deserialize the entire xml document into a variety of different objects with some sort of hierarchical structure representing the original xml document, and then extract data from those objects to put into my database based on their type. Is there any way/a better way to do this?

Answer

RAS picture RAS · Nov 13, 2012

You're looking for an ExpandoObject.
ExpandoObject is a dynamic object introduced in C#4.
Sample implementation found here:

 public static IEnumerable<dynamic> GetExpandoFromXml(string file, string descendantid)
{
    var expandoFromXml = new List<dynamic>();

    var doc = XDocument.Load(file);
    var nodes = doc.Root.Descendants(descendantid);

    foreach (var element in doc.Root.Descendants(descendantid))
    {
        dynamic expandoObject = new ExpandoObject();
        var dictionary = expandoObject as IDictionary<string, object>;
        foreach (var child in element.Descendants())
        {
            if (child.Name.Namespace == "")
                dictionary[child.Name.ToString()] = child.Value.Trim();
        }
        yield return expandoObject;
    }
}

More links:
http://www.codeproject.com/Tips/227139/Converting-XML-to-an-dynamic-object-using-ExpandoO
http://www.codeproject.com/Articles/461677/Creating-a-dynamic-object-from-XML-using-ExpandoOb