XmlSerializer - some (enum) field not getting serialized...?

Here's the scenario. I'm serializing a particular object, which contains other objects as properties. I have this particular enum type that won't get included in the serialized XML.

For illustration - I'll simplify the class (which is a property of the main class being serialized); it looks as follows:

public class Contact
{
  public ContactRoleType ContactRole;
  public string ContactID;
  // ....and some more fields...
}


The ContactRoleType is a normal enum. All fields are serialized, except the enum field ContactRole. Strange thing is, when I rename the member field ContactRole, it does get serialized.

I have actually used 
Chris Sells' excellent tool XmlSerializerPreCompiler , and all the types can be serialized without any problem.

All comments and ideas are welcomed!

Thanks!

7 Comments

  • Thanks a lot for posting about this tool!

  • I have the same problem .I user Framework 2.0!

  • Hey I have this problem too... Funny thing is, I believe it's in the same code you did! Get in touch buddy!

  • i had a similar problem with an enum property, turned out the property had to have setter as well as getter - my initial implementation required only a getter for encapsulation or whatever... :)
    check out how i solved it at my blog esdeeblog.blogspot.com

  • Hey, i too have this problem and when I rename it works. But in my case u cannot rename it as we generated it with an XSD which we got from a client and they expect the same attribute name as that of the enum. Is there any other solution other than renaming the property name ??

  • same problem in my case was related to the setting of minoccours=0 for the field. When removed (and switched to nillable=true) the field was serialized fine.

  • Actually a property does not get serialized if its value is equal to default value (if DefaultValueAttribute is specified with that property)

    like:

    [System.ComponentModel.DefaultValueAttribute(GroupType.Workspace)]
    public GroupType Type
    {
    get
    {
    return this.typeField;
    }
    set
    {
    this.typeField = value;
    }
    }

    So, if you comment/delete first line which is setting default value, the problem will be solved.

Comments have been disabled for this content.