如何序列化到dateTime

问题描述 投票:4回答:6

努力获取任何时区的DateTimes。我正在使用DateTimeOffset,一个字符串和一个XmlElement属性。当我这样做时,我收到以下错误:

[InvalidOperationException:'dateTime'是XmlElementAttribute.DataType属性的无效值。 dateTime无法转换为System.String。] System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model,String ns,ImportContext context,String dataType,XmlAttributes a,Boolean repeats,Boolean openModel,RecursionLimiter limiter)+450

[InvalidOperationException:反映类型'System.String'的错误。] System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model,String ns,ImportContext context,String dataType,XmlAttributes a,Boolean repeats,Boolean openModel,RecursionLimiter limiter)+1621 System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping访问器,FieldModel模型,XmlAttributes a,String ns,Type choiceIdentifierType,Boolean rpc,Boolean openModel,RecursionLimiter limiter)+8750 System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel parent,FieldModel model,XmlAttributes a,String ns,RecursionLimiter limiter)+139 System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping,StructModel model,Boolean openModel,String typeName,RecursionLimiter limiter)+1273

[InvalidOperationException:反映属性'creationTimeX'时出错。] ...

码:

 [System.Xml.Serialization.XmlElement(ElementName = "creationTime",
      DataType="dateTime")]
 public string creationTimeX
    {
        get
        {
            return this.creationTimeField.ToString("yyyy-MM-ddTHH:mm:sszzz");
        }
        set
        {
            DateTimeOffset.TryParse(value, out this.creationTimeField);
        }
    }

[System.Xml.Serialization.XmlIgnoreAttribute()]
public System.DateTimeOffset creationTime
{
    get {
        return this.creationTimeField;
    }
    set {
        this.creationTimeField = value;
    }
}
c# datetime .net-2.0 xml-serialization
6个回答
2
投票

看一下有关序列化日期和UTC的StackOverflow问题:

Best practices for DateTime serialization in .Net framework 3.5/SQL Server 2008

无需为了完成序列化而创建特殊属性。


3
投票

这对我有用

private const string DateTimeOffsetFormatString = "yyyy-MM-ddTHH:mm:sszzz";
private DateTimeOffset eventTimeField;

[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
public string eventTime
{
    get { return eventTimeField.ToString(DateTimeOffsetFormatString); }
    set { eventTimeField = DateTimeOffset.Parse(value); }
}

1
投票

使用XmlConvert.ToDateTimeOffset()和.ToString()方法在XmlSerializer变通方法属性中正确序列化和反序列化DateTimeOffset。

这里有Microsoft Connect文章的完整示例,并且确认不幸的是Microsoft不会修复这种疏忽(XmlSerializer本身应该支持任何原始类型):

https://connect.microsoft.com/VisualStudio/feedback/details/288349/datetimeoffset-is-not-serialized-by-a-xmlserializer


1
投票

我建议你将DateTime序列化为long(这是实现在内部用于存储实际值的内容)。

你可以使用DateTime.Ticks来获取值,它有一个需要很长的构造函数(Int64)。


0
投票

大卫

属性的数据类型(creationTime)是字符串,而Xml序列化数据类型是“dateTime”。这就是为什么你得到那个例外。

您可以通过将数据类型更改为DateTime来解决此问题

此外,对于您发布任何时区的当前时间,您必须应用DateTime.Now.ToUniveralTime并在其上应用适当的DateTimeFormat模式。

这些步骤就在这里

http://msdn.microsoft.com/en-us/library/k494fzbf.aspx

谢谢-RVZ


0
投票

这是2019年,我从这个UDateTime找到了一个很好的单一脚本,用于自定义类型和属性抽屉gist

using System;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;

// we have to use UDateTime instead of DateTime on our classes
// we still typically need to either cast this to a DateTime or read the DateTime field directly
[System.Serializable]
public class UDateTime : ISerializationCallbackReceiver {
    [HideInInspector] public DateTime dateTime;

    // if you don't want to use the PropertyDrawer then remove HideInInspector here
    [HideInInspector] [SerializeField] private string _dateTime;

    public static implicit operator DateTime(UDateTime udt) {
        return (udt.dateTime);
    }

    public static implicit operator UDateTime(DateTime dt) {
        return new UDateTime() {dateTime = dt};
    }

    public void OnAfterDeserialize() {
        DateTime.TryParse(_dateTime, out dateTime);
    }

    public void OnBeforeSerialize() {
        _dateTime = dateTime.ToString();
    }
}

// if we implement this PropertyDrawer then we keep the label next to the text field
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(UDateTime))]
public class UDateTimeDrawer : PropertyDrawer {
    // Draw the property inside the given rect
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
        // Using BeginProperty / EndProperty on the parent property means that
        // prefab override logic works on the entire property.
        EditorGUI.BeginProperty(position, label, property);

        // Draw label
        position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);

        // Don't make child fields be indented
        var indent = EditorGUI.indentLevel;
        EditorGUI.indentLevel = 0;

        // Calculate rects
        Rect amountRect = new Rect(position.x, position.y, position.width, position.height);

        // Draw fields - passs GUIContent.none to each so they are drawn without labels
        EditorGUI.PropertyField(amountRect, property.FindPropertyRelative("_dateTime"), GUIContent.none);

        // Set indent back to what it was
        EditorGUI.indentLevel = indent;

        EditorGUI.EndProperty();
    }
}
#endif
© www.soinside.com 2019 - 2024. All rights reserved.