Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm getting this message,"The string '7/22/2006 12:00:00 AM' is not a valid AllXsd value.", when deserializing an XML, the element contains a date, this is the property that is supposed to be mapped to the element:

[XmlElement("FEC_INICIO_REL",typeof(DateTime))]
public DateTime? FechaInicioRelacion { get; set; }

Am I doing something wrong?

UPDATE: Here is the XML:

<Detalle>
  <NOM_ASOC>Financiera Panameña</NOM_ASOC>
  <DESCR_CORTA_RELA>PREST. PERSONAL</DESCR_CORTA_RELA>
  <FEC_INICIO_REL>7/22/2006 12:00:00 AM</FEC_INICIO_REL>
  <FEC_FIN_REL>9/22/2008 12:00:00 AM</FEC_FIN_REL>
  <MONTO_ORIGINAL>1160.0000</MONTO_ORIGINAL>
  <NUM_PAGOS>2</NUM_PAGOS>
  <DESCR_FORMA_PAGO>PAGOS VOLUNTARIOS</DESCR_FORMA_PAGO>
  <IMPORTE_PAGO>59.9400</IMPORTE_PAGO>
  <FEC_ULTIMO_PAGO>11/15/2006 12:00:00 AM</FEC_ULTIMO_PAGO>
  <MONTO_ULTIMO_PAGO>0.0000</MONTO_ULTIMO_PAGO>
  <DESCR_OBS_CORTA />
  <SALDO_ACTUAL>1078.3900</SALDO_ACTUAL>
  <NUM_DIAS_ATRASO>0</NUM_DIAS_ATRASO>
  <HISTORIA>1</HISTORIA>
  <MONTO_CODIFICADO />
  <FEC_ACTUALIZACION>10/17/2008 12:00:00 AM</FEC_ACTUALIZACION>
  <COD_GRUPO_ECON>  </COD_GRUPO_ECON>
  <TIPO_ASOC>  </TIPO_ASOC>
  <NUM_REFER>2008628116</NUM_REFER>
</Detalle>
                Please post the XML that causes this problem. If possible, narrow this down to an example that only has a single DateTime? property. BTW, notice that typeof(DateTime?) is not the same thing as typeof(DateTime).
– John Saunders
                May 18, 2010 at 23:10

I solved the issue by storing the date in string and then creating a getter which parses the date and returns it as DateTime.

Sample code:

    [XmlElement("Valid")]
    public string _Valid
    [XmlIgnore]
    public bool? Valid
            if (!string.IsNullOrWhiteSpace(_Valid))
                return bool.Parse(_Valid);
            return null;

AllocationDate is a mandatory field but can be supplied as blank which is handled by representing it by AllocationDateString:

    private DateTime? _allocationDate;
    [XmlIgnore]
    public DateTime? AllocationDate
        get { return _allocationDate; }
        set { _allocationDate = value; }
    [XmlAttribute("AllocationDateTime")]
    public string AllocationDateTimeString
            return _allocationDate.HasValue ? XmlConvert.ToString(_allocationDate.Value, XmlDateTimeSerializationMode.Unspecified)
            : string.Empty;
            _allocationDate = !string.IsNullOrEmpty(value) ? XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.Unspecified) : (DateTime?)null;
                Got me in the right direction! In my code I don't want a nullable type, so the DateTime.Parse (used in my own code) will throw an exception if serialization fails.
– Jan_V
                Feb 15, 2017 at 9:45

I realize that this is an old question, but I had this issue today and I found a workaround using properties and casting.

private string _date; // Private variable to store XML string
// Property that exposes date. Specifying the type forces
// the serializer to return the value as a string.
[XmlElement("date", Type = typeof(string))]
public object Date {
    // Return a DateTime object
        return
            !string.IsNullOrEmpty(_date) ? 
            (DateTime?) Convert.ToDateTime(_date) : 
            null;
    set { _date = (string)value; } 

Now, whenever you need to refer to the date, you simply call:

var foo = (DateTime?)Bar.Date

It's been working fine for me since. If you don't mind adding the extra cast in your code, you can do it this way as well!

Edit: Due to Dirk's comment, I decided to revisit my implementation in a seperate branch. Rather than using an object class, which is prone to runtime compiler errors, I return the value as a string.

[XmlElement("date")] 
public string Date;

Which makes the declaration much simpler. But when attempting to read from the variable you now need to provide null checks.

var foo = string.IsNullOrEmpty(Date) ? Convert.ToDateTime(Date) : (DateTime?) null

It works the exact same way as the previous implementation, except the casting and null checks occur at a different location. I want to be able to write my model and then forget about it, so I still prefer my implementation instead.

On another note, I added a correction to the cast before the edit: DateTime should be DateTime?.

It works if used correctly. But whether it's elegant is a different question and I wouldn't call it that. It's too easy to write code that compiles fine but throws an exception at runtime because the type of the property is object. If _date contains a valid string then even an innocent looking obj.Date = obj.Date; will throw and exception. At the very least (string)value would have to be replaced by value.ToString() (with a null-check). – Dirk Mar 1, 2014 at 16:52

For those who come across this here is the simplest answer, I ran into the same issue, but didn't need nullable DateTime. The XMLElement only need a get not a set when rendering XML.

private DateTime _fechaInicioRelacion;
[XmlElement("FEC_INICIO_REL")]
public string FechaInicioRelacionString
            return _fechaInicioRelacion.ToString("yyyy-MM-ddTHH:mm:ss");
    set { }
[XmlIgnore]
public DateTime FechaInicioRelacion
    get { return _fechaInicioRelacion; }
    set { _fechaInicioRelacion = value; }
                The accepted answer recommended basically the same thing 6 years before this answer was posted.
– EJoshuaS - Stand with Ukraine
                Mar 28 at 4:51
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.