Can you specify format for XmlSerialization of a datetime?

前端 未结 2 1057
孤街浪徒
孤街浪徒 2020-11-27 06:01

I need to serialize / deserialize a datetime into yyyyMMdd format for an XML file. Is there an attribute / workaround I can use for this?

2条回答
  •  误落风尘
    2020-11-27 06:42

    No, there isn't. If it's in that format, then it's not a valid dateTime as far as XML Schema is concerned.

    The best you can do is as follows:

    [XmlIgnore]
    public DateTime DoNotSerialize {get;set;}
    
    public string ProxyDateTime {
        get {return DoNotSerialize.ToString("yyyyMMdd");}
        set {DoNotSerialize = DateTime.Parse(value);}
    }
    

提交回复
热议问题