Solved

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

Posted on 2004-08-18
8
3,069 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 14

Expert Comment

by:AvonWyss
ID: 11846683
Philyard8133, note that this is exactly what I was suggesting in my comment. ;-)
0
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…

830 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