本文介绍了在C#中将XML空日期反序列化为DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XML,需要将其反序列化为C#对象。除了日期元素(有时为空)以外,所有元素都起作用。

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>

模型类定义为:

[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;


        }
    }
}

我得到的异常是:

它不喜欢将空的日期值传递给DateTime属性。

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

当日期值为空时,如何反序列化为DateTime属性?

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

推荐答案

在XML中表示空值的方法是使用 xsi:nil 属性:

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; }

这篇关于在C#中将XML空日期反序列化为DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:30