Link to home
Start Free TrialLog in
Avatar of HydeNSeeka
HydeNSeeka

asked on

How do I accept MM/DD/YYYY date format in using XmlSerializer?

Hello.

I am accepting an XML feed from a client. I have created a class for to match it and am attempting to use the XmlSerializer class to automatically convert the feed to a binary format for use. There is a date that is sent over in MM/DD/YYYY format and my call to XmlSerializer.Deserialize is giving me the following error:

String was not recognized as a valid DateTime.

Is there an XmlAttribute I can use to make the class accept this format, or something else I can do? I'm currently using [serial.XmlElementAttribute(DataType="date")], but that is working for MM/DD/YYYY. It is working if on some test cases in which I changed the format to YYYY-MM-DD, but that is not an option for me at this point unless I transform it which does not really make sense for one field.

Thanks,

Michael Mercieca
ASKER CERTIFIED SOLUTION
Avatar of AvonWyss
AvonWyss
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Philyard8133
Philyard8133


Sorry ! I posted the wrong MyDate class, that other one will just save it like a normal Xml date, oposite of what you wanted!

public class MyDate
{
      public string dateValue;

      public DateTime GetDateValue()
      {
            return Convert.ToDateTime(dateValue);
      }

      public MyDate()
      {
      }

      public MyDate(DateTime aValue)
      {
            dateValue = aValue.ToShortDateString();
      }
}
Philyard8133, note that this is exactly what I was suggesting in my comment. ;-)
Avatar of HydeNSeeka

ASKER

Thanks guys. I was unaware of the "standard" date time format. I've given Avon 400 pts for the original answer and 100 to Philyard for taking the time to add all of the code. Your help is much appreciated.
You're welcome, and thank you very much.
Thank you HydeNSeeka!

AvonWyss - you were too quick for me! when I first check there were no comments and I didn't refresh before I posted mine :p
Sorry Philyard8133, I must admit that you certainly needed more time to write your comment than I did. I hope you aren't too disappointed about that... I wish you all the best!