XmlSerializer: The string ” is not a valid AllXsd value

Posted on

Problem :

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:

public DateTime? FechaInicioRelacion { get; set; }

Am I doing something wrong?

Here is the XML:

  <NOM_ASOC>Financiera PanameƱa</NOM_ASOC>
  <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>
  <FEC_ULTIMO_PAGO>11/15/2006 12:00:00 AM</FEC_ULTIMO_PAGO>

Solution :

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:

    public string _Valid

    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;

    public DateTime? AllocationDate
        get { return _allocationDate; }
        set { _allocationDate = value; }

    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;

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
            !string.IsNullOrEmpty(_date) ? 
            (DateTime?) Convert.ToDateTime(_date) : 
    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.

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?.

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;

public string FechaInicioRelacionString
            return _fechaInicioRelacion.ToString("yyyy-MM-ddTHH:mm:ss");
    set { }

public DateTime FechaInicioRelacion
    get { return _fechaInicioRelacion; }
    set { _fechaInicioRelacion = value; }

Try adding “IsNullable=true” attribute.

Leave a Reply

Your email address will not be published. Required fields are marked *