views:

3384

answers:

3

Hello!

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

A: 

I don't think you have any control over that. If you change it to your second form, the XML serializer will probably choke.

You can always take control of the serialization process by implementing ISerializable (not the same as decorating with the [Serializable] attribute) on your class. You also need to create a special constructor for de-serialization.

Brian Genisio
+2  A: 

Take a look at XmlAttributeOverrides class.

Sunny
+7  A: 

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); }
}
Adam Tegen
I believe you mean "this.Delivered" instead of "this.DateTime"?
GalacticCowboy
my DateTime property is nullable (DateTime?) does this change a lot?
GrZeCh