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,166
  • 11
  • 71
  • 98
GrZeCh
  • 2,292
  • 8
  • 29
  • 38

2 Answers2

20

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
  • 70,191
  • 42
  • 128
  • 155
Adam Tegen
  • 24,389
  • 33
  • 119
  • 152
1

Take a look at XmlAttributeOverrides class.

Sunny Milenov
  • 21,386
  • 5
  • 78
  • 105