Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-08-18
8
Medium Priority
?
3,376 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 14

Accepted Solution

by:
AvonWyss earned 1600 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 400 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

722 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