How to serialize empty string for integer?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Julia

    How to serialize empty string for integer?


    Hi,

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

    Thanks.


  • Paul E Collins

    #2
    Re: How to serialize empty string for integer?

    "Julia" <codewizard@012 .net.il> wrote:
    [color=blue]
    > I want to serialize an integer member as an empty
    > string when the member value is set to zero[/color]

    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 myIntToSerialis e
    {
    get
    {
    return (myInt == 0) ? "" : myInt.ToString( );
    }
    set
    {
    myInt = (value == "") ? 0 : Int32.Parse(val ue);
    }
    }

    P.


    Comment

    • Patrik Löwendahl [C# MVP]

      #3
      Re: How to serialize empty string for integer?

      Hi Julia,

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

      --
      Patrik Löwendahl [C# MVP]
      cshrp.net - 'Elegant code by witty programmers'
      cornerstone.se 'IT Training for professionals'

      Julia wrote:[color=blue]
      > Hi,
      >
      > I want to serialize an integer member as an empty string
      > when the member value is set to zero
      >
      > Thanks.
      >
      >[/color]

      Comment

      Working...