How to serialize empty string for integer?

  • Thread starter Thread starter Julia
  • Start date Start date
J

Julia

Hi,

I want to serialize an integer member as an empty string
when the member value is set to zero

Thanks.
 
Julia said:
I want to serialize an integer member as an empty
string when the member value is set to zero

You can do this by marking your integer 'XmlIgnore' and exposing a
string property instead. The string property's get and set accessors
will be called when serialising and deserialising.

[XmlIgnore] private int myInt;

public string myIntToSerialise
{
get
{
return (myInt == 0) ? "" : myInt.ToString();
}
set
{
myInt = (value == "") ? 0 : Int32.Parse(value);
}
}

P.
 
Hi Julia,

You will have to change the default serialization behaviour and
implement the ISerializable interface to change how the values are stored.
 
Back
Top