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?
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
In short, there is no way to deserialize type from XML into the concrete type named by XML content. To be able to deserialize xml into the given concrete type, you must have that type defined in your code. Or maybe have a different structure of type, which will have TypeName into it defined (from where it was created, i.e. tag name in xml).
The only thing deserialization to dynamic object gives you is, you will end up having some kind of dynamic type just having those all properties in it.
I would not deserialize XML into dynamic object if I were to save it to the database. Instead, I would use XML document object model (XmlDocument?) to read and then save to the database. Otherwise, you will end up having object with all string properties and then saving them. Seems to be just a redundant step to deserialize into object.
Besides, deserializing into dynamic object (as suggested by the answer), will not give you different types of objects (at least not with those types in XML).