Quick Tip: Common mistakes with #XML serialization in .NET/C#

If you are working with XML serialization in .NET/C#, you might have already encountered this and might have analyzed a lot why your data is not serializing though you have written proper code. The reason/solution might be simple but in case you are not aware of such common mistakes, you will definitely break your head finding the root cause.


In this quick post, we will learn about the common mistakes that may happen while declaring the properties of data class that you are going to serialize. Continue reading and let me know, if you find more points to be added here.


Quick Tip: Common mistakes with XML serialization in .NET/C# (www.kunal-chowdhury.com)


Recently, one of my friend faced this issue while writing a piece of code to serialize and de-serialize the data. He wrote the code for serialization, structured the data class with many properties but when he executed it, the object was not serializing properly as XML. He already spent a lot of his time to investigate the root cause of this issue before we sit together. We observed that, he did some common mistakes while declaring the properties in the class.


Based on this, I thought to write down few common mistakes in serialization here (which I know), so that if someone searching on net and come to this page might help them to quickly look into those issues and fix without spending a lot of their time. Also, I thought to bookmark this information in my blog for future reference.


Here is a checklist of common mistakes that you should care while serializing .NET object to XML:

    • Make sure that, you declared your property with public access modifier.
    • You must implement the getter of the property and should be public.
    • It is not mandatory to write a setter, but in case you declare it, should be specified with public access modifier.
    • The field that you are accessing directly from the getter/setter is not readonly.
    • If the class has a public bool ShouldSerializeFoo() method, make sure that, it is not returning false. In other case, the entire class will not serialize.
    • If the class implements public bool FooSpecified { get; set; } property, it’s not returning false.
    • Make sure that, the property is not marked with [DefaultValue] attribute. In case, you want to set a default value, assign it in the constructor.
    • The properties that you are exposing for serialization, is not marked with [XmlIgnore] or [Obsolete]. Here I wrote about XmlIgnore attribute: Quick Tip: How to prevent a property from #XML serialization?. You would like to read it.

I hope, this post was useful for you if you came to this page searching over the internet. Don’t forget to share your comments specifying how this save your time. Enjoy reading my other posts. Happy coding!!!


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter , Facebook , Google+ and LinkedIn . I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.


© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top