Custom DateTime XML Serialization

后端 未结 3 997
清歌不尽
清歌不尽 2020-12-21 02:15

I would like to be able to Serialize a DateTime with a specific Time Zone that is not the server, nor is it client time. Basically, any time zone. Is it possible to override

相关标签:
3条回答
  • 2020-12-21 02:32

    Don't serialize the DateTimeOffset directly, but serialize a string instead:

    // Don't serialize this one
    [System.Xml.Serialization.XmlIgnore]
    public System.DateTimeOffset metadataDateTime
    {
        get { ... }
        set { ... }
    }
    
    
    // Serialize this one instead
    [System.Xml.Serialization.XmlAttribute("metadataDateTime")]
    public string metadataDateTimeXml
    {
        get { /* format metadataDateTime to custom format */ }
        set { /* parse metadataDateTime from custom format */ }
    }
    
    0 讨论(0)
  • 2020-12-21 02:42

    This was discussed 2 days ago. Does this do it for you?

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

    0 讨论(0)
  • 2020-12-21 02:45

    You may want to use DateTimeOffset instead of a pure DateTime object.

    0 讨论(0)
提交回复
热议问题