views:

2979

answers:

3

Working to get DateTimes for any time zone. I'm using DateTimeOffset, and a string, and an XmlElement attribute. When I do, I get the following error:

[InvalidOperationException: 'dateTime' is an invalid value for the XmlElementAttribute.DataType property. dateTime cannot be converted to 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: There was an error reflecting type '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 accessor, FieldModel model, 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: There was an error reflecting property 'creationTimeX'.] ...

Code:

 [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;
    }
}
A: 

David

The datatype of the property(creationTimeX) is string while the XmlSerialization datatype is "dateTime". Thats why you are getting that exception.

You can fix this by Changing the datatype to DateTime

Also for your issue of the current time for any timezone, you would have to apply a DateTime.Now.ToUniveralTime and apply appropriate DateTimeFormat pattern on it.

The steps for these are here

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

Thanks -RVZ

Not what I need.I would like to override the standard DateTime, so that we can specify ANY time zome.... eg DateTimeOffset.Specifing DataType for strings works for positiveInteger, nonPositiveInteger etc... but does not work for datetimethanks
david valentine
+1  A: 

Take a look at this StackOverflow question about serializing dates and UTC:

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

No need to create a special property just to accomplish the serialization.

Jason Jackson
The comment says it all.. even thought ISO time allows anything... If you absolutely, positively must know the timezone itself (i.e. the above could be Eastern Standard Time or Central Daylight Time), you need to create your own datatype which exposes those pieces.Implement iXmlSerializer
david valentine
A: 

I would suggest you serialize DateTime as a long (which is what the implementation uses internally to store the actual value).

You can use DateTime.Ticks to get the value and it has a ctor that takes a long (Int64)

Asher