bignis
asked on
Deserialize XML to a DateTime
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. XmlElement Attribute( Form=Syste m.Xml.Sche ma.XmlSche maForm.Unq ualified)]
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.ToDa teTime(Str ing s, String[] formats) +103
System.Xml.Serialization.X mlCustomFo rmatter.To DateTime(S tring value, String[] formats) +9
System.Xml.Serialization.X mlCustomFo rmatter.To DateTime(S tring value) +30
System.Xml.Serialization.X mlSerializ ationReade r.ToDateTi me(String value) +5
Microsoft.Xml.Serializatio n.Generate dAssembly. XmlSeriali zationRead er1.Read6_ KeyCiteHis toryDocume nt(Boolean isNullable, Boolean checkType) +867
Microsoft.Xml.Serializatio n.Generate dAssembly. XmlSeriali zationRead er1.Read1_ KeyCiteHis tory(Boole an isNullable, Boolean checkType) +606
Microsoft.Xml.Serializatio n.Generate dAssembly. XmlSeriali zationRead er1.Read10 _KeyCiteHi story() +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!
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()
[System.Xml.Serialization.
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
System.Xml.XmlConvert.ToDa
System.Xml.Serialization.X
System.Xml.Serialization.X
System.Xml.Serialization.X
Microsoft.Xml.Serializatio
Microsoft.Xml.Serializatio
Microsoft.Xml.Serializatio
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!
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
hi bignis,
I got into the same situation as yours. Can you provide me with the solution?
Thanks
I got into the same situation as yours. Can you provide me with the solution?
Thanks
source : http://tinyurl.com/48c4o
--------------------
One way to handle this is to decorate the DateTime member with
[System.Xml.Serialization.
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.M
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