Deserializing XML Into A List Or Array Of Objects

This blog is in continuation of my other blog. In the first blog, I explained how to use XML attribute overrides to reduce the number of attributes required in source code, and how to centralize some common functionality in a single location (the blog focused on how to serialize to XML using camel casing as default for tag names). I will be referring to code from that first blog, so I do recommend you to read that one first.

That said, let’s get into that topic: REST Services many times return a collection, a list of objects. When the format is JSON, the returned text will look something like the following.

  1.    [ { prop1 = “x”, prop2 = “y’} , { prop1 = “a”, prop2 = “b”}, …..]  

When the response is XML, the code might look like this.

  1. <allCustomers> 
  2.    <myCustomer> 
  3.       <prop1>x</prop1>
  4.       <prop2>y</prop2>
  5.    </myCustomer>
  6.    <myCustomer> 
  7.       etc 
  8.    </myCustomer>
  9. </allCustomers> 

In order to deserialize this content, people see the need to create a helping class, like this one.

  1. [XmlRoot(ElementName = "allCustomer")]  
  2. public class ListOfCustomers : List<MyCustomer>  
  3. {  
  4. }  

So, a real project ends up having many classes like this one: Empty, and repetitive. Well, when something is repetitive, there must be a better way. And when something is empty.... yikes! Awful!

In this case, there is a solution. All these empty classes can be avoided with the use of Attribute overrides. Basically, the deserializer needs to know that a tag is associated with a type. And the emphasis here is in the word type. It does not need to be a class. It needs to be a type. And List<MyCustomer> is a type. So, that’s a step forward. The other piece of the puzzle is telling the deserializer that whenever it sees tag “allCustomers”, it must deserialize into this type.

Here is the tricky part: Marking class MyCustomer with an XmlRoot attribute won’t do the trick. The root is of type List<MyCustomer>, so an XmlRoot attribute tied to MyCustomer does nothing.

If the object of this class were a property, then it would be marked, for example,  with XmlElement attribute.  But it's not an element. Here, MyCustomer plays more the role of XmlArrayItem than anything else. But a class cannot be marked with an XmlArrayItem attribute. This is reserved for class members. So, then, how is it done? The right way is to mark the class with attribute XmlType. Adding this tells the deserializer that this class exists, and it’s a type to be used by other types. Without this definition in the "bag of overrides", the list will come out empty when deserialized.

There is another problem to solve - Whether “MyCustomer” has an XmlRoot attribute or not. There has to be a way to tell the deserializer the tag name to use when finding a list. It could be programmed in the attribute override code to be setting all such tags to be something like listOfxxxxx where xxxxx is the name of the generic element. But this solution would be restrictive. This will definitely not help in many real life scenarios. Another way is to create a new attribute

  1.    [XmlObjectWrapper(ElementName = "allCustomers")]  

This attribute will be used when processing the overrides. It will tell the code that generates the overrides: “Hey, if you ever need to deserialize a list, or array of these type of object, the surrounding tag name should be allCustomers”. So you do need to add this new attribute to the classes which you know will become members of a list to be deserialized.

The definition of the new attribute is quite simple: 

  1. namespace MyProject  
  2. {  
  3.     using System;  
  4.   
  5.     public class XmlObjectWrapperAttribute : Attribute  
  6.     {  
  7.         public string ElementName { getset; }  
  8.     }  
  9. }  

Here is the definition of class MyCustomer now including this attribute: 

  1. [XmlObjectWrapper(ElementName = "allCustomers")]  
  2. public class MyCustomer  
  3. {  
  4.     public string NameAndLastName { getset; }  
  5.     public int Age { getset; }  
  6.     public Double Height { getset; }  
  7.     public Gender Gender { getset; }  
  8.     public List<Book> ReadingPile { getset; }  
  9.     public Boolean Active { getset; }  
  10.   
  11.     [XmlElement(ElementName = "WEEKLY_MONEY")]  
  12.     public Decimal Allowance { getset; }  
  13.   
  14.     public MyCustomer ReferredBy { getset; }  
  15. }  

Here, I repeat the “CreateAttributeOverrides” method, which I published in my previous blog. This one is the improved version which handles deserialization of objects into Lists of something. I have highlighted the relevant code.

  1. public static XmlAttributeOverrides CreateAttributeOverrides(Type objectType)  
  2. {  
  3.     Func<Type, bool> IsList = t => t.IsGenericType &&   
  4.         t.GetGenericTypeDefinition().IsAssignableFrom(typeof(List<>));  
  5.   
  6.     HashSet<Type> workTypes = GetTypesToOverride(objectType);  
  7.     XmlAttributeOverrides theBag = new XmlAttributeOverrides();  
  8.     var classNames = new HashSet<string>();  
  9.     classNames.Add(objectType.FullName);  
  10.   
  11.     while (workTypes.Count > 0)  
  12.     {  
  13.         Type singleType = workTypes.First();  
  14.         workTypes.Remove(singleType);  
  15.         if (HasXmlAttributes(singleType))  
  16.             continue;  
  17.   
  18.         var wrapperAttr = singleType.GetCustomAttribute<XmlObjectWrapperAttribute>(true/*inherit*/);  
  19.         if (wrapperAttr != null)  
  20.         {  
  21.             string wrapperTagName = wrapperAttr.ElementName;  
  22.             Type listType = typeof(List<>);  
  23.             Type listOfTType = listType.MakeGenericType(singleType);  
  24.             string elementTagName = singleType.Name.ToCamel();  
  25.             theBag.Add(  
  26.                 singleType,  
  27.                 new XmlAttributes()   
  28.                 {   
  29.                     XmlType = new XmlTypeAttribute(elementTagName),  
  30.                     XmlRoot = new XmlRootAttribute("OUTEROBJECT")  
  31.                 });  
  32.   
  33.             theBag.Add(  
  34.                 listOfTType,  
  35.                 new XmlAttributes() { XmlRoot = new XmlRootAttribute(wrapperTagName) });  
  36.   
  37.             classNames.Add(singleType.FullName);  
  38.             workTypes.Remove(singleType);  
  39.         }  
  40.         else if (singleType == objectType)  
  41.         {  
  42.             theBag.Add(objectType, new XmlAttributes() {XmlRoot = new XmlRootAttribute("OUTEROBJECT") });  
  43.         }  
  44.   
  45.         PropertyInfo[] allPropsInfo = singleType.GetProperties();  
  46.         foreach (PropertyInfo propInfo in allPropsInfo)  
  47.         {  
  48.             HashSet<Type> overridableTypes = GetTypesToOverride(propInfo.PropertyType);  
  49.             bool propHasXmlAttributes = HasXmlAttributes(propInfo);  
  50.             if (propHasXmlAttributes)  
  51.                 overridableTypes.Remove(propInfo.PropertyType);  
  52.   
  53.             List<String> overridableNames = overridableTypes.Select(t => t.FullName).ToList();  
  54.             overridableTypes.RemoveWhere(t => classNames.Contains(t.FullName));  
  55.             classNames.UnionWith(overridableTypes.Select(t => t.FullName));  
  56.             workTypes.UnionWith(overridableTypes);  
  57.             if (propHasXmlAttributes)  
  58.                 continue;  
  59.   
  60.             string camelName = propInfo.Name.ToCamel();  
  61.             var propOverrides = new XmlAttributes();  
  62.             Type propType = propInfo.PropertyType;  
  63.             if (propType.IsArray || IsList(propType))  
  64.             {  
  65.                 string pascalName = propInfo.Name.ToPascal();  
  66.                 propOverrides.XmlArray = new XmlArrayAttribute("COLLECTION" + pascalName);  
  67.                 propOverrides.XmlArrayItems.Add(new XmlArrayItemAttribute(camelName));  
  68.             }  
  69.             else  
  70.             {  
  71.                 propOverrides.XmlElements.Add(new XmlElementAttribute(camelName));  
  72.             }  
  73.   
  74.             theBag.Add(singleType, propInfo.Name, propOverrides);  
  75.         }  
  76.     }  
  77.   
  78.     return theBag;  
  79. }  
And now, the final few pieces to demonstrate the code is working. First, some code to deserialize XML strings to objects: 
  1. public static T Deserialize<T>(string s) 
  2. {  
  3.     T returnValue = default(T);  
  4.     Type returnType = typeof(T);  
  5.     XmlAttributeOverrides xmlOverrides = CreateAttributeOverrides(returnType);  
  6.     XmlSerializer serializer = new XmlSerializer(returnType, xmlOverrides);  
  7.     using (TextReader reader = new StringReader(s))  
  8.     {  
  9.         returnValue = (T)serializer.Deserialize(reader);  
  10.     }  
  11.   
  12.     return returnValue;  
  13. }  

Next, the test code which exercises all the above code.  Despite being coded as a test, it's not really a test.  It just helps illustrate the execution of the above code.

  1. [Test]  
  2. public void TestDeserializeIntoList()  
  3. {  
  4.     string serializedListOfCustomers = File.ReadAllText("c:\\temp\\serialized.txt", Encoding.UTF8);  
  5.     List<MyCustomer> custList = XmlCamelOverrides.Deserialize<List<MyCustomer>>(serializedListOfCustomers);  
  6.     Console.WriteLine("custList has {0} elements.", custList.Count);  
  7. }  

Finally, a text file with serialized code to deserialize. Note that all the tags use camel casing, just the same as in my previous blog.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <allCustomers  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >  
  3.     <myCustomer>  
  4.       <nameAndLastName>Lee Leevan</nameAndLastName>  
  5.       <age>35</age>  
  6.       <height>6.01</height>  
  7.       <gender>Male</gender>  
  8.       <COLLECTIONReadingPile>  
  9.         <readingPile>  
  10.           <title>C# for Dummies</title>  
  11.           <color>Yellow</color>  
  12.         </readingPile>  
  13.         <readingPile>  
  14.           <title>The Black Box</title>  
  15.           <color>Orange</color>  
  16.         </readingPile>  
  17.         <readingPile>  
  18.           <title>The Red Sea</title>  
  19.           <color>White</color>  
  20.         </readingPile>  
  21.       </COLLECTIONReadingPile>  
  22.       <active>true</active>  
  23.       <WEEKLY_MONEY>21.61</WEEKLY_MONEY>  
  24.       <referredBy>  
  25.         <nameAndLastName>Leann Lunn</nameAndLastName>  
  26.         <age>32</age>  
  27.         <height>6.01</height>  
  28.         <gender>Female</gender>  
  29.         <COLLECTIONReadingPile />  
  30.         <active>false</active>  
  31.         <WEEKLY_MONEY>21.61</WEEKLY_MONEY>  
  32.       </referredBy>  
  33.     </myCustomer>  
  34. </allCustomers>  

Run the code. The XML will deserialize into a List<MyCustomer>, with 1 element.



Time to remove all the empty classes and make code simpler. 

Conclusion

While XML attribute overrides are not easy to learn and use, they are powerful, and as I showed here, they open the doors to do things which reduce the amount of code you need to write. Less code means your code is cleaner, easier to understand, and more maintainable. Less code duplication means that if there is a bug, it will be fixed in one location, not hundreds of places.

Next Recommended Reading Deserialize XML with Array Node In C#