TimeSpan serialization.

  • Thread starter Thread starter Kevin Burton
  • Start date Start date
K

Kevin Burton

I have two properties that I am trying to serialize:

[XmlAttribute(AttributeName="last_updated")]
public DateTime LastUpdated
{
get
{
return lastUpdated;
}
set
{
lastUpdated = value;
}
}
[XmlAttribute(AttributeName="elapsed",
DataType="dateTime")]
public TimeSpan Elapsed
{
get
{
return timeSpan;
}
set
{
timeSpan = value;
}
}


The first, a DateTime seems to serialize just fine. The
second, a TimeSpan gives me the following exception:

There was an error reflecting property 'Elapsed'.
at
System.Xml.Serialization.XmlReflectionImporter.ImportStruct
LikeMapping(Str
uctModel model, String ns)
at
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMa
pping(TypeModel
model, String ns, ImportContext context, String dataType,
Boolean repeats)
-----------------------
Cannot serialize member 'Elapsed'. XmlAttribute/XmlText
cannot be used to encode
complex types.
at
System.Xml.Serialization.XmlReflectionImporter.ImportAccess
orMapping(Membe
rMapping accessor, FieldModel model, XmlAttributes a,
String ns, Type choiceIden
tifierType)
at
System.Xml.Serialization.XmlReflectionImporter.ImportFieldM
apping(StructMo
del parent, FieldModel model, XmlAttributes a, String ns)
at
System.Xml.Serialization.XmlReflectionImporter.ImportStruct
LikeMapping(Str
uctModel model, String ns)
-----------------------

I was under the impression that there was very little
different between DateTime and TimeSpan. Has anyone been
able to serialize and deserialize a TimeSpan structure?

Thank you.

Kevin
 
Try this:
[XmlAttribute(elapsed, typeof(TimeSpan))]
public TimeSpan Elapsed
{
get
{
return timeSpan;
}
set
{
timeSpan = value;
}
}

TimeSpan is not a DateTime or any structure that represents a date, rather
an interval representation, ultimately a number. So you cannot serialize as
XSD dateTime, like you tried. (maybe you can serialize as "long" but I
didn't try it)

HTH
 
Thank you for your feedback.

I have already tried the obvious:

[XmlAttribute(AttributeName="elapsed", Type=typeof
(TimeSpan))]
public TimeSpan Elapsed
{
get
{
return timeSpan;
}
set
{
timeSpan = value;
}
}

Same exception.

Kevin
-----Original Message-----
Try this:
[XmlAttribute(elapsed, typeof(TimeSpan))]
public TimeSpan Elapsed
{
get
{
return timeSpan;
}
set
{
timeSpan = value;
}
}

TimeSpan is not a DateTime or any structure that represents a date, rather
an interval representation, ultimately a number. So you cannot serialize as
XSD dateTime, like you tried. (maybe you can serialize as "long" but I
didn't try it)

HTH

--
TJoker, MCSD.NET
MVP: Paint, Notepad, Solitaire

****************************************


I have two properties that I am trying to serialize:

[XmlAttribute(AttributeName="last_updated")]
public DateTime LastUpdated
{
get
{
return lastUpdated;
}
set
{
lastUpdated = value;
}
}
[XmlAttribute(AttributeName="elapsed",
DataType="dateTime")]
public TimeSpan Elapsed
{
get
{
return timeSpan;
}
set
{
timeSpan = value;
}
}


The first, a DateTime seems to serialize just fine. The
second, a TimeSpan gives me the following exception:

There was an error reflecting property 'Elapsed'.
at
System.Xml.Serialization.XmlReflectionImporter.ImportStruct
LikeMapping(Str
uctModel model, String ns)
at
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMa
pping(TypeModel
model, String ns, ImportContext context, String dataType,
Boolean repeats)
-----------------------
Cannot serialize member 'Elapsed'. XmlAttribute/XmlText
cannot be used to encode
complex types.
at
System.Xml.Serialization.XmlReflectionImporter.ImportAccess
orMapping(Membe
rMapping accessor, FieldModel model, XmlAttributes a,
String ns, Type choiceIden
tifierType)
at
System.Xml.Serialization.XmlReflectionImporter.ImportFieldM
apping(StructMo
del parent, FieldModel model, XmlAttributes a, String ns)
at
System.Xml.Serialization.XmlReflectionImporter.ImportStruct
LikeMapping(Str
uctModel model, String ns)
-----------------------

I was under the impression that there was very little
different between DateTime and TimeSpan. Has anyone been
able to serialize and deserialize a TimeSpan structure?

Thank you.

Kevin


.
 
See
http://www.dotnet247.com/247reference/msgs/25/128430.aspx

Can you serialize the number of Ticks in the timespan, eg

public class TestMe {

private System.TimeSpan timeSpan;

// for serialization only
[System.Xml.Serialization.XmlAttribute(AttributeName="elapsedticks")]
public long ElapsedTicks {
get { return timeSpan.Ticks;}
set { timeSpan= new System.TimeSpan(value);}
}

[System.Xml.Serialization.XmlIgnore]
public System.TimeSpan Elapsed {
get { return timeSpan; }
set { timeSpan = value; }
}

public TestMe() {
System.DateTime s= System.DateTime.Now;
System.Random r= new System.Random();
System.Threading.Thread.Sleep(r.Next(1122)+455); // in milliseconds
System.DateTime f= System.DateTime.Now;
timeSpan= f.Subtract(s);
}

public static void Main() {

TestMe t= new TestMe();
t.Doit();
}

public void Doit() {
//Serialize
System.Xml.Serialization.XmlSerializer s = new
System.Xml.Serialization.XmlSerializer(typeof(TestMe));
s.Serialize(System.Console.Out,this); // serialize self to Console.Out

}

}




Kevin Burton said:
Thank you for your feedback.

I have already tried the obvious:

[XmlAttribute(AttributeName="elapsed", Type=typeof
(TimeSpan))]
public TimeSpan Elapsed
{
get
{
return timeSpan;
}
set
{
timeSpan = value;
}
}

Same exception.

Kevin
-----Original Message-----
Try this:
[XmlAttribute(elapsed, typeof(TimeSpan))]
public TimeSpan Elapsed
{
get
{
return timeSpan;
}
set
{
timeSpan = value;
}
}

TimeSpan is not a DateTime or any structure that represents a date, rather
an interval representation, ultimately a number. So you cannot serialize as
XSD dateTime, like you tried. (maybe you can serialize as "long" but I
didn't try it)

HTH

--
TJoker, MCSD.NET
MVP: Paint, Notepad, Solitaire

****************************************


I have two properties that I am trying to serialize:

[XmlAttribute(AttributeName="last_updated")]
public DateTime LastUpdated
{
get
{
return lastUpdated;
}
set
{
lastUpdated = value;
}
}
[XmlAttribute(AttributeName="elapsed",
DataType="dateTime")]
public TimeSpan Elapsed
{
get
{
return timeSpan;
}
set
{
timeSpan = value;
}
}


The first, a DateTime seems to serialize just fine. The
second, a TimeSpan gives me the following exception:

There was an error reflecting property 'Elapsed'.
at
System.Xml.Serialization.XmlReflectionImporter.ImportStruct
LikeMapping(Str
uctModel model, String ns)
at
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMa
pping(TypeModel
model, String ns, ImportContext context, String dataType,
Boolean repeats)
-----------------------
Cannot serialize member 'Elapsed'. XmlAttribute/XmlText
cannot be used to encode
complex types.
at
System.Xml.Serialization.XmlReflectionImporter.ImportAccess
orMapping(Membe
rMapping accessor, FieldModel model, XmlAttributes a,
String ns, Type choiceIden
tifierType)
at
System.Xml.Serialization.XmlReflectionImporter.ImportFieldM
apping(StructMo
del parent, FieldModel model, XmlAttributes a, String ns)
at
System.Xml.Serialization.XmlReflectionImporter.ImportStruct
LikeMapping(Str
uctModel model, String ns)
-----------------------

I was under the impression that there was very little
different between DateTime and TimeSpan. Has anyone been
able to serialize and deserialize a TimeSpan structure?

Thank you.

Kevin


.
 
Back
Top