Enum not serializing

32,324

Solution 1

The property was protected. set it to Public and viola - serialized the enum property. Kinda bad as the property resides in a bass class....rather have it protected

Solution 2

Try this.

[Serializable]
public enum EnumToSerialize
{
    [XmlEnum("1")]
    One = 1,
    [XmlEnum("2")]
    Two = 2
}

Solution 3

Try this article on MSDN. This example seems to be able to set a property with an enumeration and serialize it. You should be able to get that same value back when de-serializing the object.

Share:
32,324
Ahmed ilyas
Author by

Ahmed ilyas

a previous MSFT, C# MVP and have had my name published in a few books as a technical reviewer. Have my own consultancy company in the UK and the US. Used to contribute heavily on the old MSDN forums and beginning to again as well as engaging with SO from time to time. Love to help the community gain great knowledge and give out my best to them, help people learn the right from the wrong, implement best practices and get people/developers to where they want to be. I soley focus on the Microsoft platform... SOreadytohelp

Updated on March 21, 2020

Comments

  • Ahmed ilyas
    Ahmed ilyas about 4 years

    I have a WCF service. it is bound to an MSMQ but that is not the issue here. I can serialize an object which has a base class and an interface implemented in the base class and the concrete class derives from the base class - this works fine.

    however, when I have an enum in the base class and I set that value, then after it being deserialized/read from the MSMQ, that value is still set to the default value (i.e not the one set manually in code)

    any ideas whats going on? I even marked the enum as a DataContract and also each of the Enum members with an EnumMember attribute.

    how can I serialize enums?