Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

XmlSerializer.Deserialize and DateTime problem

3,389 views
Skip to first unread message

Sebastian

unread,
Jun 9, 2003, 8:07:08 AM6/9/03
to
Hi,

I've got and XSD schema, with dates using this type:

<xs:simpleType name="DateType">
<xs:restriction base="xs:date">
<xs:pattern value="\d{4}-\d{2}-\d{2}" />
</xs:restriction>
</xs:simpleType>
I made both dataset and (xmlSerializable) classes
using xsd.exe and that xsd file. Now when I collect
data into dataset, and write it as xml dates
(DataTime type in DataSet) are saved in other format:
YYYY-MM-DDT00:00:00.0000000+02:00 ('s' format?)

Now the problem is, that XmlSerializer can't deserialize
such xml into objects, instances of classes generated by
xsd.exe using the same xsd file.

What I get is:

System.InvalidOperationException: There is an error in
XML document (3, 25). ---> System.FormatException: String
was not recognized as a valid DateTime.
at System.DateTime.ParseExact(String s, String[]
formats, IFormatProvider provider, DateTimeStyles style)
at System.Xml.XmlConvert.ToDateTime(String s, String[]
formats)
at
System.Xml.Serialization.XmlCustomFormatter.ToDateTime
(String value, String[] formats)
at System.Xml.Serialization.XmlCustomFormatter.ToDate
(String value)
at
System.Xml.Serialization.XmlSerializationReader.ToDate
(String value)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializa
tionReader1.Read2_Report(Boolean isNullable, Boolean
checkType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializa
tionReader1.Read1_Response(Boolean isNullable, Boolean
checkType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializa
tionReader1.Read7_Response()
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize
(XmlReader xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize
(XmlReader xmlReader)
at xsd_ds_serialize_to_object.Form1.button3_Click
(Object sender, EventArgs e) in c:\documents and
settings\sebastian\my documents\visual studio
projects\xsd_ds_test\xsd_ds_serialize_to_object\form1.cs:l
ine 243

Funny, but System.Xml.XmlConvert.ToDateTime() works fine
with that format. What "String[] formats" are passed to
XmlConvert.ToDateTime by XmlCustomFormatter?

So, am I able to influence the datetime format dataset
is using while saving xml? (WriteXML) Tried dataset's
Locale.DateTimeFormat, but most properties are read
only :-( I assume that ds uses SortableDateTimePattern?

Or, how do I explain XmlSerializer, that the date time
produced with WriteXML is valid date?

Any help appreciated,

Sebastian


Dino Chiesa [MSFT]

unread,
Jun 9, 2003, 12:25:54 PM6/9/03
to
One way to handle this is to decorate the DateTime member with
[System.Xml.Serialization.XmlIgnore]

This tells the serializer to not serialize or de-serialize it at all.

Then, add an additional property to the class, called, for example
DateString. It might be defined as
public string DateString {
set { ... }
get { ... }
}

Then you can serialize and de-ser the DateString in the get/set logic:

public string DateString {
set {
// parse value here - de-ser from your chosen format
// use constructor, eg, Timestamp= new System.DateTime(....);
// or use one of the static Parse() overloads of System.DateTime()
}
get {
return Timestamp.ToString("yyyy.MM.dd"); // serialize to whatever format
you want.
}
}

Within the get and set you are manipulating the value of the Date member,
but you are doing it with custom logic. The serialized property need not
be a string of course, but that is a simple way to do it. You could also
ser/de-ser using an int, as with the unix epoch, for example.

-Dino

"Sebastian" <b...@NOSPAM.gazeta.pl> wrote in message
news:074f01c32e7f$a630b4c0$a101...@phx.gbl...

Sebastian

unread,
Jun 10, 2003, 7:27:35 AM6/10/03
to
Hi,

>One way to handle this is to decorate the DateTime
member with
> [System.Xml.Serialization.XmlIgnore]
>
>This tells the serializer to not serialize or de-
serialize it at all.
>
>Then,

[...]

Well, it works :-) Thanks for replying.

I might also add expression based string field in
dataset, and do the same magic (disable saving to
xml the original datetime, and savin' the string
field), am I right?

The question that still bothers me is, why doesn't
the standard way work?

Why, XmlConvert.ToDateTime(String s, String[] formats)
fails when called by XmlCustomFormatter.ToDate()?
What formats does it pass to XmlConvert?
(And XmlConvert.TodateTime(String d) works fine when
called by me manually)

But, maybe I shouldn't be that curious :-D

Bye

Dino Chiesa [MSFT]

unread,
Jun 11, 2003, 1:04:14 PM6/11/03
to
> Well, it works :-) Thanks for replying.

Glad to help.

> I might also add expression based string field in
> dataset, and do the same magic (disable saving to
> xml the original datetime, and savin' the string
> field), am I right?

Yes, I think so. why not? but I'm not sure I understand what you intend.
Anyway, by now you have probably tested it and found that it works ;)

> The question that still bothers me is, why doesn't
> the standard way work?

I don't know the answer to this one.

> But, maybe I shouldn't be that curious :-D

Don't worry, be happy.

> Bye


0 new messages