参数为null,具体取决于xml格式

时间:2015-09-28 17:46:05

标签: c# asp.net xml web-services asp.net-web-api2

我有这堂课:

[XmlRoot(ElementName = "order")]
public class Order
{
    [XmlElement(ElementName= "buyer-accepts-marketing")]
    public bool BuyerAcceptsMarketing { get; set; }

    [XmlElement(ElementName= "closed-at")]
    public DateTime? ClosedAt { get; set; }
}

这个简单的Controller用这个方法我用来测试Request:

public class OrdersController : ApiController
{
    [HttpPost]
    public HttpResponseMessage Post([FromBody]Order order)
    {
        return Request.CreateResponse(HttpStatusCode.OK);
    }
}

当我在本地运行服务并使用Fiddler将此xml发布到服务时: <?xml version="1.0" encoding="UTF-8"?> <order> <buyer-accepts-marketing type="boolean">false</buyer-accepts-marketing> </order> 我在Controller的Post方法中收到参数,一切正常。 但是,如果我使用第二个属性发布xml:

<?xml version="1.0" encoding="UTF-8"?> <order> <buyer-accepts-marketing type="boolean">false</buyer-accepts-marketing> <closed-at type="datetime" nil="true"></closed-at> </order>

参数order在Controller的方法中为空。

我可能在xml或类的“映射”中做错了,但我看不出它是什么。

1 个答案:

答案 0 :(得分:0)

您已将nil属性放在默认命名空间中。您需要将其放在"http://www.w3.org/2001/XMLSchema-instance"命名空间中。执行此操作的传统方法是将其放在带有xsi前缀的根元素中:

<?xml version="1.0" encoding="UTF-8"?>
<order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <buyer-accepts-marketing type="boolean">false</buyer-accepts-marketing>
  <closed-at type="datetime" xsi:nil="true"></closed-at>
</order>

但是在语义上将它放在"closed-at"元素本身上是相同的:

<?xml version="1.0" encoding="UTF-8"?>
<order >
  <buyer-accepts-marketing type="boolean">false</buyer-accepts-marketing>
  <closed-at xmlns:i="http://www.w3.org/2001/XMLSchema-instance" type="datetime" i:nil="true"></closed-at>
</order>

由于DateTime? ClosedAt不为空(因为它没有正确的xsi:nil="true"属性),但也无法反序列化为DateTime(因为该值为空),{{ 1}}混淆并引发异常。

<强>更新

如果您无法将XML更改为包含standard representation for a null DateTime,则需要实现代理属性以手动执行转换。由于您未在问题中指定是否需要序列化以及反序列化,因此以下两者都有:

XmlSerializer

如果您永远不需要序列化,则可以使用字符串值代理属性:

[XmlRoot(ElementName = "order")]
public class Order
{
    [XmlElement(ElementName = "buyer-accepts-marketing")]
    public bool BuyerAcceptsMarketing { get; set; }

    [XmlIgnore]
    public DateTime? ClosedAt { get; set; }

    [XmlElement("closed-at")]
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public XmlDateTimeWrapper XmlDateTime
    {
        get
        {
            return ClosedAt;
        }
        set
        {
            ClosedAt = value;
        }
    }
}

public class XmlDateTimeWrapper
{
    public static implicit operator DateTime?(XmlDateTimeWrapper wrapper) { return wrapper == null ? null : wrapper.DateTime; }

    public static implicit operator XmlDateTimeWrapper(DateTime? dateTime) { return new XmlDateTimeWrapper { DateTime = dateTime }; }

    [XmlIgnore]
    public DateTime? DateTime { get; set; }

    [XmlText]
    public string DateTimeString
    {
        get
        {
            if (DateTime == null)
                return string.Empty;
            return XmlConvert.ToString(DateTime.Value, XmlDateTimeSerializationMode.Utc);
        }
        set
        {
            if (String.IsNullOrWhiteSpace(value))
                DateTime = null;
            else
                DateTime = XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.Utc);
        }
    }

    [XmlAttribute("type")]
    public string Type { get { return "datetime"; } set { /* Do nothing */ } }

    [XmlAttribute("nil")]
    public bool Nil { get { return DateTime == null; } set { /* Do nothing */ } }
}