How XML Attribute Overrides Help Serialize Objects With Camel Case

XML is still widely used in applications. My recent usage of XML comes mostly from REST services. REST service responses nowadays use JSON formatting for the most part, but XML is still used as well. Most services, I have ever dealt with, use camel casing for naming properties, that is, a property name has a name like customerName, and not CustomerName (with a capital C).

This write-up shows how to apply a pattern to Xml serialization and deserialization (in this example, camel casing) without having to use a ton of attributes.

For my example, I have created a simple silly class with multiple property types. I will show how to serialize it using a mix of XML attributes and attribute overrides.

Why would I want to do this?
Because, in a real life scenario, it frees me from writing thousands of attributes next to thousands of object properties. I’d rather push the behavior one level deeper into the logic stack, and reduce the amount of code duplication, and the places that may require fixes when the code has a bug. This will become clearer by the conclusions of this blog.

I have defined a few classes with a variety of member types. Notice that property allowance has an XmlElement attribute (WEEKLY_MONEY) attached to it. There are occasions when the serialization tag names have to be different than the property name. For these cases, you will need to add an attribute in code, just like I did with “Allowance”. Here is the code that defines the classes to be serialized:

  1. public enum Gender  
  2. {  
  3.     Female, Male  
  4. }  
  6. public class Book  
  7. {  
  8.     public string Title { getset; }  
  9.     public string Color { getset; }  
  10. }  
  12. public class MyCustomer  
  13. {  
  14.     public string NameAndLastName { getset; }  
  15.     public int Age { getset; }  
  16.     public Double Height { getset; }  
  17.     public Gender Gender { getset; }  
  18.     public List<Book> ReadingPile { getset; }  
  19.     public Boolean Active { getset; }  
  21.     [XmlElement(ElementName = "WEEKLY_MONEY")]  
  22.     public Decimal Allowance { getset; }  
  24.     public MyCustomer ReferredBy { getset; }  
  25. }  
Notice the simplicity of how these classes are defined: XML attributes are only minimally present. And the property naming in the class definition follows the C# naming convention (PascalCasing), even though it will be serialized using camel casing.

In order to serialize this object to XML, using camel casing and without polluting my code with repetitive attributes that in real life happens thousands of times (ad nauseum), I have used XML attribute overrides. These are not simple to use. I spent quite some time getting them to work. But patience pays off. Thousands of lines of code will be saved.

XML attribute overrides, by definition, will do just that: override any attribute present in a class or property. If the attribute is not present, it’s added anyway. But, I want the behavior to be the inverse of this, i.e., the overrides to be the normal behavior, and any attributes present in code to “override the override”. To accomplish this, I need to check for attributes for every processed property and class. This is done using reflection. Wherever I find an attribute in code, I do not apply the override.

Some notes on the simplicity of some of my code- my attribute detection is poor. I am just checking for attributes whose name starts with “Xml”. This is not real life code. Just to illustrate how a feature is used.

Finally, before moving on to the code, I will give a very brief explanation of how XML override attributes work. The minimum necessary knowledge to get the job done:

There are many classes in dot Net related to the topic of attribute overrides, but the 2 most important ones are: XmlAttributeOverrides and XmlAttributes (both plural). The first is “the bag” of definitions which I will send to the serializer. It tells the serializer “Hey, this is how I want you to serialize the classes I specify here”. For any class not in “the bag”, the serializer will apply the default behavior. The second object, (XmlAttributes) is a collection of attributes that must be included for each class, and for each property. XmlAttributes defines the behavior (whether a property is an XmlElement attribute, XmlRoot, or XmlArray with XmlArrayItem, etc.). So the rule is to create one XmlAttributes object for each class, and one for each property in the class. Once these are defined, they are all thrown into the bag (added to XmlAttributeOverrides) to be sent to the serializer.

The following code adds the attributes for all the properties in my classes using reflection. This code does not cover every scenario under the sun, so if you use it, most likely you will have to consider a few more cases to match your application needs, and apply more logic for handling exceptions and attribute detection.

This example,

  • Adds an XmlRoot attribute to the outmost element
  • Sets a default tag name which is the camel case representation of the property name.
  • If a property has an Xml attribute attached to it, it will not change its serializing behavior (no overrides will be applied)
  • Will wrap collection items (List and Array only) with a “COLLECTIONxxx” tag.
  • Uses UPPER case in places with the intention of highlighting things in the serialized output. Just for ease of finding the important parts. Not the right formatting for a real life scenario.

The code uses some small “helper” methods, which I list here first. You may want to skip them and refer to them after you study the main code, if you feel you still need to check them. These helpers include a few simple string overrides as well.


  1. public static class Overrides  
  2. {  
  3.     public static string ToPascal(this string s) { return ChangeCasing(s, Char.ToUpper); }  
  4.     public static string ToCamel(this string s) { return ChangeCasing(s, Char.ToLower); }  
  6.     private static string ChangeCasing(string s, Func<Char, Char> convert)   
  7.     {   
  8.         return string.IsNullOrWhiteSpace(s) ? s : string.Format("{0}{1}", convert(s[0]), s.Substring(1));   
  9.     }  
  10. }  
  1. private static HashSet<Type> GetTypesToOverride(Type objectType)  
  2. {  
  3.     var returnValue = new HashSet<Type>();  
  4.     returnValue.Add(objectType);  
  5.     Type elementType = objectType.GetElementType();  
  6.     if (elementType != null)  
  7.         returnValue.UnionWith(GetTypesToOverride(elementType));  
  9.     objectType.GetGenericArguments()  
  10.         .Where(t => t != null)  
  11.         .ToList()  
  12.         .ForEach(t => returnValue.UnionWith(GetTypesToOverride(t)));  
  14.     returnValue.RemoveWhere(t => t == null || t.FullName.StartsWith("System"));  
  15.     return returnValue;  
  16. }  
  18. private static bool HasXmlAttributes(MemberInfo minfo)  
  19. {  
  20.     List<Attribute> xmlAttributes = minfo.GetCustomAttributes()  
  21.         .Where(t => {  
  22.             string typeName = t.GetType().Name;  
  23.             return typeName.StartsWith("Xml") && !"XmlObjectWrapperAttribute".Equals(typeName);  
  24.         })  
  25.         .ToList();  
  27.     return xmlAttributes.Count > 0;  
  28. }  
In the above code, MemberInfo type was used for “HasXmlAttributes”. This allows the method to handle arguments of type Type or PropertyInfo. They both have MemberInfo base class.


  1. public static XmlAttributeOverrides CreateAttributeOverrides(Type objectType)  
  2. {  
  3.     Func<Type, bool> IsList = t => t.IsGenericType &&  
  4.         t.GetGenericTypeDefinition().IsAssignableFrom(typeof(List<>));  
  6.     HashSet<Type> workTypes = GetTypesToOverride(objectType);  
  7.     XmlAttributeOverrides theBag = new XmlAttributeOverrides();  
  8.     var classNames = new HashSet<string>();  
  9.     classNames.Add(objectType.FullName);  
  11.     while (workTypes.Count > 0)  
  12.     {  
  13.         Type singleType = workTypes.First();  
  14.         workTypes.Remove(singleType);  
  15.         if (HasXmlAttributes(singleType))  
  16.             continue;  
  18.         if (singleType == objectType)  
  19.             theBag.Add(objectType, new XmlAttributes()   
  20.             {   
  21.                 XmlRoot = new XmlRootAttribute("OUTEROBJECT")   
  22.             });  
  24.         PropertyInfo[] allPropsInfo = singleType.GetProperties();  
  25.         foreach (PropertyInfo propInfo in allPropsInfo)  
  26.         {  
  27.             HashSet<Type> overridableTypes = GetTypesToOverride(propInfo.PropertyType);  
  28.             bool propHasXmlAttributes = HasXmlAttributes(propInfo);  
  29.             if (propHasXmlAttributes)  
  30.                 overridableTypes.Remove(propInfo.PropertyType);  
  32.             List<String> overridableNames = overridableTypes  
  33.                 .Select(t => t.FullName)  
  34.                 .ToList();  
  36.             overridableTypes.RemoveWhere(t => classNames.Contains(t.FullName));  
  37.             classNames.UnionWith(overridableTypes.Select(t => t.FullName));  
  38.             workTypes.UnionWith(overridableTypes);  
  39.             if (propHasXmlAttributes)  
  40.                 continue;  
  42.             string camelName = propInfo.Name.ToCamel();  
  43.             var propOverrides = new XmlAttributes();  
  44.             Type propType = propInfo.PropertyType;  
  45.             if (propType.IsArray || IsList(propType))  
  46.             {  
  47.                 string pascalName = propInfo.Name.ToPascal();  
  48.                 propOverrides.XmlArray = new XmlArrayAttribute("COLLECTION" + pascalName);  
  49.                 propOverrides.XmlArrayItems.Add(new XmlArrayItemAttribute(camelName));  
  50.             }  
  51.             else  
  52.             {  
  53.                 propOverrides.XmlElements.Add(new XmlElementAttribute(camelName));  
  54.             }  
  56.             theBag.Add(singleType, propInfo.Name, propOverrides);  
  57.         }  
  58.     }  
  60.     return theBag;  
  61. }  
So, this code does what I described. Some description of non-obvious things,
  • There is a set called workTypes where the classes to be serialized are stored as they are progressively found when going through the outermost class properties. Items are removed from the set once overrides are applied for them. Processing ends when the set is empty.

  • There is a HashSet<String> called classNames where all processed classes are kept. If a class is already in the “bag”, then it should not be added again. This set helps handle this functionality. Plus if there are circular dependencies in some property, this will avoid an infinite loop. No items are ever removed from this set.

  • If the class starts with “System”, then, it’s a dot Net class, and it does not need to be processed. If a system class uses non system classes (e.g. Array[MyClass1], List[MyClass2}), then the non-system generic classes will be included for processing.

  • All attribute overrides are added until workTypes is empty.

That’s it. The return type is the overrides to be sent to the serializer. And now, I will show how to call that serializer. The following code does the job.

I assume my result will be small, and so, serializing into a String is not be too bad of a fault. The method below is a static member of a class I named XmlCamelSerializer, and which is not shown here (just a wrapper for this method and the one above - the one that creates the overrides).
  1. public static string Serialize(object obj)  
  2. {  
  3.     string returnValue = null;  
  4.     Type objectType = obj.GetType();  
  5.     XmlAttributeOverrides xmlOverrides = CreateAttributeOverrides(objectType);  
  6.     XmlSerializer serializer = new XmlSerializer(objectType, xmlOverrides);  
  7.     StringBuilder sbReturn = new StringBuilder();  
  8.     using (MemoryStream memStream = new MemoryStream())  
  9.     using (StreamWriter writer = new StreamWriter(memStream))  
  10.     {  
  11.         serializer.Serialize(writer, obj);  
  12.         returnValue = Encoding.UTF8.GetString(  
  13.             memStream.ToArray(), 0, (int)memStream.Length);  
  14.     }  
  16.     return returnValue;  
  17. }  
And now, how to exercise it? I use NUnit3 for testing (easy and fast to use), so I wrote a test, although it’s not really a test. It’s just a way to exercise my code conveniently.
  1.     [Test]  
  2.     public void TestCustomer()  
  3.     {  
  4.         var cust2 = new MyCustomer()  
  5.         {  
  6.             Age = 32,  
  7.             Gender = Gender.Female,  
  8.             NameAndLastName = "Leann Lunn",  
  9.             Height = 6.01,  
  10.             ReadingPile = new List<Book>(),  
  11.             Allowance = 21.61m  
  12.         };  
  14.         var cust = new MyCustomer()  
  15.         {  
  16.             NameAndLastName = "Lee Leevan",  
  17.             Age = 35,  
  18.             Gender = Gender.Male,  
  19.             Height = 6.01,  
  20.             ReferredBy = cust2,  
  21.             Active = true,  
  22.             Allowance = 21.61m,  
  23.             ReadingPile = new List<Book> {  
  24.                 new Book() { Title = "C# for Dummies", Color = "Yellow" },  
  25.                 new Book() { Title = "The Black Box", Color = "Orange" },  
  26.                 new Book() { Title = "The Red Sea", Color = "White" }  
  27.             }  
  28.         };  
  30.         string serFoo = XmlCamelOverrides.Serialize(cust);  
  31.         Console.WriteLine(serFoo);  
  33.     }  
  34. }  
Below is the output from the serialization. Notice how properties have camel casing, and I avoided the massive proliferation of attributes in my code.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <OUTEROBJECT xmlns:xsi="" xmlns:xsd="">  
  3.   <nameAndLastName>Lee Leevan</nameAndLastName>  
  4.   <age>35</age>  
  5.   <height>6.01</height>  
  6.   <gender>Male</gender>  
  7.   <COLLECTIONReadingPile>  
  8.     <readingPile>  
  9.       <title>C# for Dummies</title>  
  10.       <color>Yellow</color>  
  11.     </readingPile>  
  12.     <readingPile>  
  13.       <title>The Black Box</title>  
  14.       <color>Orange</color>  
  15.     </readingPile>  
  16.     <readingPile>  
  17.       <title>The Red Sea</title>  
  18.       <color>White</color>  
  19.     </readingPile>  
  20.   </COLLECTIONReadingPile>  
  21.   <active>true</active>  
  22.   <WEEKLY_MONEY>21.61</WEEKLY_MONEY>  
  23.   <referredBy>  
  24.     <nameAndLastName>Leann Lunn</nameAndLastName>  
  25.     <age>32</age>  
  26.     <height>6.01</height>  
  27.     <gender>Female</gender>  
  28.     <COLLECTIONReadingPile />  
  29.     <active>false</active>  
  30.     <WEEKLY_MONEY>21.61</WEEKLY_MONEY>  
  31.   </referredBy>  
  32. </OUTEROBJECT>  
As you can see, the code attribute ("WEEKLY_MONEY") was not overridden to camle case because the XML attribute in the class definition was respected.  The root attribute was applied (the root node has tag "OUTEROBJECT"), and all other nodes were serialized using camel casing.

When you see lots of duplicate code, find a better way. Seeing a lot of XML and JSON attributes in the code led me to find how to avoid the massive code duplication and find simplicity. In this case, XmlAttributeOverrides came to the rescue. It was not easy. Getting this to work may take a while because the documentation is not easy to understand, the class names probably not the best, and its use is anything but intuitive. But the final result has very few lines of code.

These few lines of code will surely avoid thousands of lines of repetitive code in a system where serialization and deserialization are heavily used (REST service clients, for example). I will soon follow this blog with an explanation of how to serialize List responses to produce a List<T> object instead of having to create useless and silly empty wrapper classes when a collection is returned.