The string '3/18/09 10:16 PM' is not a valid AllXsd value

74,420

Solution 1

Xml readers generally expect dates/times in a very specific format; you can use this yourself using XmlConvert:

string s = XmlConvert.ToString(DateTime.Now);
DateTime when = XmlConvert.ToDateTime(s);

If you are using something else, you'll have to read it as a string and use DateTime.TryParseExact (or similar) to specify the actual format string:

string s = reader.ReadContentAsString();
DateTime when = DateTime.ParseExact(s, "M/d/yy hh:mm tt",
     CultureInfo.InvariantCulture);

If you are using XmlSerializer, you could use a shim property to do the conversion - let me know if this is what you are doing...

Solution 2

According to the XML schema spec, date time values should be in ISO8601 format, e.g., something like

2009-03-13T22:16:00
Share:
74,420
user72603
Author by

user72603

Updated on October 08, 2020

Comments

  • user72603
    user72603 over 3 years

    Obviously the reader doesn't like this format incoming from the response XML.

    Wondering if I can reformat this. Trying to convert to DateTime using the following code with my XmlReader:

    reader.ReadContentAsDateTime();
    
  • Brian Leeming
    Brian Leeming over 11 years
    The data I was feeding in happened to be 10/29/2012 15:25 and that failed. When I changed it to 10/11/2012 15:25, it worked.
  • burhan
    burhan about 10 years
    @MarcGravell, when i try to deserialize some rss feed using XmlSerializer, PubDate element causes error. how can i fix it?
  • Marc Gravell
    Marc Gravell about 10 years
    @burhan by looking at what the value is coming in as, and handling it appropriately? Alternatively, the core framework includes RSS-targeted classes that may do a better job.
  • burhan
    burhan about 10 years
    @MarcGravell, it gives the error System.InvalidOperationException. can you give the codes to fix this manually?
  • Marc Gravell
    Marc Gravell about 10 years
    @burhan not without seeing the xml and your model, no
  • burhan
    burhan about 10 years
    pls visit this link . my codes use the same algorithm.
  • burhan
    burhan about 10 years
    @MarcGravell, did you visit that link? if so, what is the solution? (someone has the same problem in the comments at the end of the page)
  • Marc Gravell
    Marc Gravell about 10 years
    @burhan the solution is, as always, to have xml and a model that match. Sometimes that means you can go direct - you might need to have the dates as a string type (rather than DateTime) and then do additional post-processing on it to parse the time. A random link, however, does not provide the exact xml you are struggling with.
  • Zubin
    Zubin about 10 years
    In ruby use iso8601 method, eg Time.now.iso8601.
  • ajiang
    ajiang almost 9 years
    Out of curiosity, ISO8601 format should look something like 2009-03-18T22:16:00-05:00, but David's answer (omitting the timezone) works for me too (whereas the ISO8601 format doesn't). Is this because of a local serialization in the XmlConverter (which therefore doesn't need the timezone)?
  • David Norman
    David Norman almost 9 years
    In ISO8601, the timezone is optional, so both my original string and your string are valid ISO8601 date time. According to w3.org/TR/xmlschema-2/#deviantformats, the timezone is also optional in XML schema. I'm not sure why the time zone version doesn't work for you.
  • userlond
    userlond over 8 years
    For php Artisans:) there is toIso8601String method in nesbot/carbon php lib.
  • relipse
    relipse almost 5 years
    in php you can use date('c', $time);