Solved

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

Posted on 2004-08-18
8
3,033 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# HTTP GET method sample code 3 60
How would you add MULTITHREADING to the attached C# code? 4 63
Hey Audio! 3 30
Create a WebAPI using a generic return parameter 9 25
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 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