Prevent timezone conversion on deserialization of DateTime value

前端 未结 4 446
离开以前
离开以前 2020-12-01 10:40

I have a class that I serialize/deserialize using XmlSerializer. This class contains a DateTime field.

When serialized, the DateTime

4条回答
  •  独厮守ぢ
    2020-12-01 11:08

    Could you try something like this post suggests and make a new string property and XmlIgnore the existing one:

    Put [XmlIgnore] on the Time property.

    Then add a new property:

    [XmlElement(DataType="string",ElementName="Time")]
    public String TimeString
    {
       get { return this.timeField.ToString("yyyy-MM-dd"); }
       set { this.timeField = DateTime.ParseExact(value, "yyyy-MM-dd", CultureInfo.InvariantCulture); }
    }
    

提交回复
热议问题