Deserializing XML null dates to DateTime in C#

一曲冷凌霜 提交于 2020-06-27 13:57:48

问题


I have the following XML which needs to be deserialized into C# objects. All the elements work except for date elements which sometimes are empty.

<?xml version="1.0" encoding="utf-8" ?>
<Output xmlns:b="http://webservices.mycompany.com/Order/17.2.0">
    <b:RequestedCompletionDate>
      <State>Modified</State>
      <Action>DateSpecified</Action>
      <Date></Date>
    </b:RequestedCompletionDate>
</Output>

The model class is defined as:

[System.Xml.Serialization.XmlType(Namespace = "http://webservices.mycompany.com/Order/17.2.0", AnonymousType = true)]
[System.Xml.Serialization.XmlRoot(Namespace = "http://webservices.mycompany.com/Order/17.2.0", IsNullable = false)]
public partial class RequestedCompletionDate
{

    private string stateField;

    private string actionField;

    private DateTime? dateField;

    /// <remarks/>

    [System.Xml.Serialization.XmlElement(Namespace = "http://webservices.mycompany.com/Framework/17.2.0")]
    public string State
    {
        get
        {
            return this.stateField;
        }
        set
        {
            this.stateField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElement(Namespace = "http://webservices.mycompany.com/Framework/17.2.0")]
    public string Action
    {
        get
        {
            return this.actionField;
        }
        set
        {
            this.actionField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElement(Namespace = "http://webservices.mycompany.com/Framework/17.2.0")]
    public DateTime? Date
    {
        get
        {
            return this.dateField;
        }
        set
        {

            this.dateField = value;


        }
    }
}

The exception I get is:

"The string '' is not a valid AllXsd value."

It doesn't like passing a null date value to a DateTime property.

How can I deserialize to a DateTime property when the date value is empty?


回答1:


The way to represent null values in XML is with an xsi:nil attribute:

<Output xmlns:b="http://webservices.mycompany.com/Order/17.2.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <b:RequestedCompletionDate>
      <State>Modified</State>
      <Action>DateSpecified</Action>
      <Date xsi:nil="true"></Date>
    </b:RequestedCompletionDate>
</Output>

If your input doesn't have that, then you can deserialize it to a string and handle the conversion in a non-serialized property:

[XmlIgnore]
public DateTime? Date
{
    get
    {
        DateTime dt;
        if(DateTime.TryParse(SerialDate, out dt))
        {
            return dt;
        }

        return null;
    }
    set
    {
        SerialDate = (value == null) 
            ? (string)null 
            : value.Value.ToString("yyyy-MM-ddTHH:mm:ss");
    }
}

[System.Xml.Serialization.XmlElement("Date", Namespace = "http://webservices.mycompany.com/Framework/17.2.0")]
public string SerialDate { get; set; }


来源:https://stackoverflow.com/questions/46162195/deserializing-xml-null-dates-to-datetime-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!