I have this :
[Flags]
public enum InfoAbonne{civilite,name,firstname,email,adress,country }
public class Formulaire
{
private InfoAbonne _infoAbonne{ get; set;}
public Formulaire()
{}
}
I want to Xml serialize Formulaire
If I init :_infoAbonne = InfoAbonne.name | InfoAbonne.email;
In my Xml Result I get only :
<InfoAbonne>email</InfoAbonne>
Even though you added the Flags
attribute to your enum, you still need to make sure that the values are powers of two:
[Flags]
public enum InfoAbonne
{
civilite = 1,
name = 2,
firstname = 4,
email = 8,
adress = 16,
country = 32
}
See the guidelines laid out in the Remarks section of the documentation.
The basic idea with these sorts of problems is to serialize a backing field which mimics the field you want to serialize. Same principle can be applied to complex types such as Bitmaps etc... For instance, instead of serializing the Enum field directly, you could serialize a backing field of type int:
// Disclaimer: Untested code, both in execution and compilation
[Flags]
public enum InfoAbonne
{
civilite = 0x1, // Increment each flag value by *2 so they dont conflict
Name=0x2,
firstname=0x4,
email=0x8,
adress=0x10,
country=0x20
}
// Don't serialize this property
[XmlIgnore]
private InfoAbonne _infoAbonne { get; set;}
// Instead serialize this property as integer
// e.g. name | email will equal 0xA in hex, or 10 in dec
[XmlElement("InfoAbonne")]
public int InfoAbonneSerializer
{
get { return (int)_infoAbonne; }
set { _infoAbonne= (InfoAbonne) value; }
}
Best regards,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With