Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# serializing Class to XML where one of class properties is DateTime. How to make this property in ISO format?

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

like image 565
GrZeCh Avatar asked Nov 18 '08 17:11

GrZeCh


2 Answers

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); }
}
like image 193
Adam Tegen Avatar answered Oct 27 '22 03:10

Adam Tegen


Take a look at XmlAttributeOverrides class.

like image 30
Sunny Milenov Avatar answered Oct 27 '22 04:10

Sunny Milenov