11

I'm serializing class which contains DateTime property.

public DateTime? Delivered { get; set; }

After serializing Delivered node contains DateTime formatted like this:

2008-11-20T00:00:00

How can I change this property to make it look like this:

2008-11-20 00:00:00

Thanks in advance

alexandrul
  • 12,856
  • 13
  • 72
  • 99
GrZeCh
  • 2,332
  • 8
  • 29
  • 38

2 Answers2

21

The hack I use for odd formatting during XmlSerialization is to have a special property that is only used during XmlSerialization

//normal DateTime accessor
[XmlIgnore]
public DateTime Delivered { get; set; }

//special XmlSerialization accessor
[XmlAttribute("DateTime")]
public string XmlDateTime
{
    get { return this.Delivered.ToString("o"); }
    set { this.Delivered = new DateTime.Parse(value); }
}
David Basarab
  • 72,212
  • 42
  • 129
  • 156
Adam Tegen
  • 25,378
  • 33
  • 125
  • 153
1

Take a look at XmlAttributeOverrides class.

Sunny Milenov
  • 21,990
  • 6
  • 80
  • 106