开发者

XmlSerializer: The string '' is not a valid AllXsd value

开发者 https://www.devze.com 2022-12-30 14:26 出处:网络
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 map

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_RE开发者_StackOverflowL>
  <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>


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
    {
        get;
        set;
    }

    [XmlIgnore]
    public bool? Valid
    {
        get
        {
            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
    {
        get
        {
            return _allocationDate.HasValue ? XmlConvert.ToString(_allocationDate.Value, XmlDateTimeSerializationMode.Unspecified)
            : string.Empty;
        }
        set
        {
            _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
    get
    {
        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?.


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
{
    get
        {
            return _fechaInicioRelacion.ToString("yyyy-MM-ddTHH:mm:ss");
        }
    set { }
}

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


Try adding "IsNullable=true" attribute.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号