The Overview
Here’s a quick example of how you can deserialize XML into C# objects. This will be brief, so feel free to ask questions in the comments for more details.
We are going to write a small class and program to deserialize objects.xml into a MyObject object that can then be used in the main program, Program. Its not rocket science, but it beats the hell out of manually parsing XML to get the information we need.
The Code
objects.xml
> > > ObjectName>> 123>> 2010-12-15T08:54:11>> ridiculously long name> >> > AnotherObject>> 235435>> 2010-12-15T08:59:51>> seriously, its way too long> >> > TheLastObject>> >> 2010-12-15T09:04:43>> no way this is gonna be the property name> > >
MyObject.cs
using System; using System.Xml.Serialization; namespace MyNamespace { public class MyObject { public string name; public int intValue; public DateTime timestamp; [XmlElement("elementNameDifferentThanObjectPropertyName")] public string extraValue; public MixOrder() { name = ""; intValue= 0; timestamp = new DateTime(); extraValue = ""; } } }
Notice I used the XmlElement attribute for the XML field with the obscenely long name. XmlElement specifically represents System.Xml.Serialization.XmlElementAttribute. This allows us to assign an element or attribute of the XML to a public property of the deserialized object even if their names don’t match. This is useful in the case of undesirable XML element names that are hard to work with or break coding conventions. Also, and more importantly, as the XML you deserialize changes over time, you can just adjust the name of the XmlElement attribute if necessary without changing the functionality of the object.
Program.cs
using System; using System.Xml; using System.Xml.Serialization; using MyNamespace; namespace TestConsoleApplication { class Program { static void Main(string[] args) { List<MyObject> myObjects = new List<MyObject>(); XmlSerializer serializer = new XmlSerializer(typeof(List<MyObject>)); XmlReader reader = XmlReader.Create("objects.xml"); myObjects = (List<MyObject>)serializer.Deserialize(reader); // now you can perform operations on your list of MyObject objects, // no manual XML parsing necessary. Console.WriteLine("Presskey to exit." ); Console.ReadLine(); } } }
I leave the processing of the object up to you, but the process is clear. We create a XmlSerializer that corresponds to the type of our object, create a XmlReader for the XML, then use the serializer to deserialize the XML from the reader. From there you have a successfully populated List
In the words of the Beastie Boys, “That’s it, that’s all, that’s all there is.” See ya at the next quick tip.