Solved

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

Posted on 2004-08-18
8
2,969 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:HydeNSeeka
  • 4
  • 3
8 Comments
 
LVL 14

Accepted Solution

by:
AvonWyss earned 400 total points
ID: 11836597
The official XML date format is YYYY-MM-DD, and I don't know how you could tell the serializer to convert this as you need it. But why not store the date as as string in your binary object (which would allow for proper deserialization) and use a member method to convert it to a DateTime when needed?
0
 

Assisted Solution

by:Philyard8133
Philyard8133 earned 100 total points
ID: 11836752
Hi,

I was curious because I'm just diving into Xml myself and so I did a little research and I think I was able to duplicate your problem. I found a blog entry that appeared to sum up your dilema (http://weblogs.asp.net/kaevans/archive/2003/05/07/6643.aspx)

W3C dictates that "the XML data type date requires the format YYYY-MM-DD." (w3schools.com)

I'm not sure how you are reading the xml in but depending on how flexible you can be you could define the field as a string type or else create a class that will read your prefered date format:


using System;
using System.Collections;
using System.IO;
using System.Xml.Serialization;

public class Group
{
      /* Set the element name and namespace of the XML element.
      By applying an XmlElementAttribute to an array,  you instruct
      the XmlSerializer to serialize the array as a series of XML
      elements, instead of a nested set of elements. */
      
      [XmlElement(ElementName = "MyDate")]
      public MyDate myDate;
 
}  

public class MyDate
{
       public DateTime dateValue;

      public MyDate()
      {
      }
      public MyDate(DateTime aValue)
      {
            dateValue = aValue;
      }
}

public class Run
{
      public static void Main()
      {
            Run test = new Run();
            test.SerializeObject("FirstDoc.xml");
            test.DeserializeObject("FirstDoc.xml");

            Console.ReadLine();
      }


      public void SerializeObject(string filename)
      {

            // Create the XmlSerializer.
            XmlSerializer s = new XmlSerializer(typeof(Group));

            // To write the file, a TextWriter is required.
            TextWriter writer = new StreamWriter(filename);

            /* Create an instance of the group to serialize, and set
               its properties. */
            Group group = new Group();
            
            group.myDate = new MyDate(DateTime.Today);
            
            // Serialize the object, and close the TextWriter.      
            s.Serialize(writer, group);
            writer.Close();
      }

      public void DeserializeObject(string filename)
      {
            FileStream fs = new FileStream(filename, FileMode.Open);
            XmlSerializer x = new XmlSerializer(typeof(Group));
            Group g = (Group) x.Deserialize(fs);
            Console.WriteLine(g.myDate.dateValue.ToShortDateString());
                        
      }
}


Hope that helps a little bit anyway!
-Philyard8133
0
 

Expert Comment

by:Philyard8133
ID: 11845896

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();
      }
}
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 11846683
Philyard8133, note that this is exactly what I was suggesting in my comment. ;-)
0
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.

 

Author Comment

by:HydeNSeeka
ID: 11846731
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.
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 11846804
You're welcome, and thank you very much.
0
 

Expert Comment

by:Philyard8133
ID: 11846950
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
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 11847972
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!
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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
A short film showing how OnPage and Connectwise integration works.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

930 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

12 Experts available now in Live!

Get 1:1 Help Now