Paul Evans is a senior programmer in Austin, TX that loves to build great things. Before moving from the UK he worked for Lionhead Studios on tools for the Fable Franchise, Milo and other unreleased titles. He is currently freelancing in Austin after spending some time at Portalarium. You can find him on Linkedin, see other things he has written on his personal blog and You can also follow him on twitter @PaulECoyote. Everything in this article is Paul's opinion alone and does not necessarily reflect his employers views, nor constitute a legal relationship. Contact Paul if you are working on anything interesting!
Posts by Paul Evans
  1. ActionScript Tips for Programmers of C#/C++ ( Counting comments... )
  2. Idempotent - A Word in the Cloud ( Counting comments... )
  3. Behind the Scenes of Bit By Bit Games First Release ( Counting comments... )
  4. Travel Tips from an Englishman in Texas ( Counting comments... )
  5. Let me introduce you to Gameduino ( Counting comments... )
  6. Micro Talks ( Counting comments... )
  7. The XmlSerializer class in C# is asymmetric ( Counting comments... )
  8. JS1K Oregon Trail. Games & Demos in 1k of Javascript ( Counting comments... )
  9. Indies, how do you make a living? ( Counting comments... )
  10. Pop your head up ( Counting comments... )
  11. (Audio Post) Round Table: DLC ( Counting comments... )
  12. Do or do not ( Counting comments... )
  13. Cover Fire for Coders ( Counting comments... )
  14. Cats have nine lives ( Counting comments... )

C# coders choosing XmlSerializer for their game data beware.  When using the DefaultValueAttribute on members of a class being later processed by XmlSerializer, a perfect copy of an object written may not be read back later.  This post demonstrates the behavior.

XmlSerializer versus DefaultValueAttribute

The DefaultValue attribute is useful for giving hints to the user interface controls, particularly ones that inspect objects such as the PropertyGrid.  It allows objects to be created using their  default constructor then have their default value set later by the control responsible for it.

The XmlSerializer also takes this value in to consideration when writing an xml representation of the object.  If the class member being serialized has a value that matches the value declared by the DefaultValue attribute it skips writing the value all together.  Less written, less to read back – looks good.

XmlSerializer does not restore the value of DefaultValue to a member when reading an xml representation of an object, it uses the default as defined by the member type (null for classes, 0 for integers, etc.). This is the potential source of confusion; you cannot rely on the XmlSerializer to create perfect deep copies of your objects. It does not assume missing xml means “use the default value attribute”. XmlSerializer has implicitly set the expectation on your code to restore the default values from the attributes.


I have embedded commented code in the post below that demonstrates the above.  I have also uploaded it to github.

// See Clue class to change demo behavior.
// Uncomment one of the fixes below to observe different behaviors.
// Search for FIX1 or FIX2 to see where the changes have been made.
//// #define FIX1  // Do *not* use DefaultValue attribute, taken in to account serializing but not de-serializing.
//// #define FIX2  // Set default value of variable to same as DefaultValue attribute during construction.
namespace AsymmetricXmlSerializer
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.IO;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    /// <summary>
    /// Class to be serialized and demo point for asymmetric XmlSerializer behavior.
    /// </summary>
    public class Clue
        /// <summary>
        /// Comment out DefaultValue attribute and it will succeed.
        /// </summary>
        /// <remarks>Set variable to 'Nothing To See Here'to to work around.</remarks>
#if !FIX1
        [DefaultValue("Nothing To See Here")]
#if !FIX2
        public string text;
        public string text = "Nothing To See Here";
    /// <summary>
    /// Demo program for asymmetric XmlSerializer behavior with DefaultValueAttribute.
    /// </summary>
    internal sealed class Program
        public static void Main(string[] args)
            // If initial value is the same as DefaultValueAttribute for variable declaration,
            // it is not persisted by XmlSerializer.
            Clue clue = new Clue();
            clue.text = "Nothing To See Here";
            // Buffer for writing.
            StringBuilder buffer = new StringBuilder();
            // Don't need namespaces.
            XmlSerializerNamespaces no_namespaces = new XmlSerializerNamespaces();
            no_namespaces.Add(string.Empty, string.Empty);
            // Don't need xml declaration.
            XmlWriterSettings writer_settings = new XmlWriterSettings() { OmitXmlDeclaration = true };
            // Serialise the clue...
            XmlSerializer xml_serializer = new XmlSerializer(typeof(Clue));
            StringWriter string_writer = new StringWriter(buffer);
            using (XmlWriter xml_writer = XmlWriter.Create(string_writer, writer_settings))
                xml_serializer.Serialize(xml_writer, clue, no_namespaces);
            string serialized = buffer.ToString();
            // Show serialised version of the clue...
            Console.WriteLine("Clue text value: {0}", clue.text);
            Console.WriteLine("Clue serialized: {0}", serialized);
            // Deserialise the clue...
            Clue clue_read;
            using (StringReader sr = new StringReader(serialized))
                clue_read = (Clue)xml_serializer.Deserialize(sr);
            // Show clue 2 on console
            // Reset and reuse write buffer.
            buffer.Length = 0;
            using (XmlWriter xml_writer = XmlWriter.Create(string_writer, writer_settings))
                xml_serializer.Serialize(xml_writer, clue_read, no_namespaces);
            string reserialized = buffer.ToString();
            Console.WriteLine("Read Clue text value: {0}", clue_read.text);
            Console.WriteLine("Read Clue serialized: {0}", reserialized);
            Debug.Assert(clue.text == clue_read.text, "Written & read clue's text should match");
 * FIX1: Don't use DefaultValueAttribute.
 * You would want to use DefaultValueAttribute to avoid persisting the
 * default value to the xml representation.
 * Although this does prevent the default value being persisted, on read the
 * DefaultValueAttribute is ignored so the value is never set to that of
 * the DefaultValueAttribute.
 * DefaultValueAttribute is also used for value hints for UI.
 * See:
 * FIX2: Use DefaultValueAttribute AND default value at construction.
 * To use DefaultValueAttribute and have the object be recreated
 * properly when reading it back, you must to explicitly set the
 * default value during construction of the object being read.
 * Using this method means the xml is concise and the object
 * is accurately recreated.