Solved

Deserialize XML to a DateTime

Posted on 2004-09-15
6
4,958 Views
Last Modified: 2008-10-05
Hi everyone!

This is a hopefully easy question but I can't find much useful information on the Net.

I have an XML document that has this node:

<Date>7/18/1980</Date>

I have a C# program that calls XmlSerializer.Deseralize().  The class definition contains this:

[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public DateTime Date;

So I want the Date XML node to resolve to a C# DateTime object.  But I get this when I run it:
System.FormatException: String was not recognized as a valid DateTime.


[FormatException: String was not recognized as a valid DateTime.]
   System.DateTime.ParseExact(String s, String[] formats, IFormatProvider provider, DateTimeStyles style) +106
   System.Xml.XmlConvert.ToDateTime(String s, String[] formats) +103
   System.Xml.Serialization.XmlCustomFormatter.ToDateTime(String value, String[] formats) +9
   System.Xml.Serialization.XmlCustomFormatter.ToDateTime(String value) +30
   System.Xml.Serialization.XmlSerializationReader.ToDateTime(String value) +5
   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read6_KeyCiteHistoryDocument(Boolean isNullable, Boolean checkType) +867
   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read1_KeyCiteHistory(Boolean isNullable, Boolean checkType) +606
   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read10_KeyCiteHistory() +73

Is it possible to modify the attribute so it can parse the date in mm/dd/yyyy format?  If not, how must I structure my XML so it's valid?

Thanks in advance!
0
Comment
Question by:bignis
6 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 12064947
there is a post here that could help you in this case
source : http://tinyurl.com/48c4o

--------------------
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

by Dino Chiesa
--------------------

HTHAB
0
 

Author Comment

by:bignis
ID: 12065929
I'm unclear on the contents of your post.  In particular the line "// parse value here - de-ser from your chosen format".  Can you fill in the code for this part?  I can't see how I can deserialize automatically from within a property.
0
 
LVL 7

Accepted Solution

by:
NipNFriar_Tuck earned 50 total points
ID: 12075240
If you make your field private and your property public then the XML Serializer will use your property to answer you last statement.

From what I read on your code I think your issue may be in your formatting of the date, by default I believe that the XML Serializer uses dd/mm/yyyy and since there is no 18th month your date is invalid.

0
 

Author Comment

by:bignis
ID: 12380073
I'd like to give points to NipNFriar_Tuck.  His answer regarding dd/mm/yyyy format didn't answer my question per-se, but it provided me with the solution I needed.
0
 

Expert Comment

by:cctest
ID: 22645782
hi bignis,
I got into the same situation as yours. Can you provide me with the solution?
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now