无法将WebAPI XML响应编组到C#对象中

时间:2013-03-19 15:55:11

标签: c# asp.net asp.net-web-api

我有一些XML从REST服务回来,我在编组到C#类时遇到问题。

XML看起来像这样:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SCResponse>
    <link href="http://192.168.6.126:8001/affiliate/account/81/notifications?startRow=2&amp;limit=20" rel="next_page" title="Next"/>
    <link href="http://192.168.6.126:8001/affiliate/account/81/notifications?startRow=1&amp;limit=20" rel="previous_page" title="Previous"/>
    <RecordLimit>20</RecordLimit>
    <Notifications>
        <Notification href="http://192.168.6.126:8001/affiliate/account/81/notifications/24">
            <NotificationDate>2013-03-15T16:41:37-05:00</NotificationDate>
            <NotificationDetails>Notification details 1</NotificationDetails>
            <Status>New</Status>
            <NotificationTitle>Test notification 1</NotificationTitle>
        </Notification>
    </Notifications>
    <RecordsReturned>1</RecordsReturned>
    <StartingRecord>1</StartingRecord>
    <TotalRecords>1</TotalRecords>
</SCResponse>

并且'粘贴为特殊'自动生成的类如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
using System.Xml.Serialization;

namespace CMINet.Models.notifications
{


/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]

// to make it work I must replace the above with:
//    [XmlType]
//    [DataContract(Namespace = "")]


public partial class SCResponse
{

    private SCResponseLink[] linkField;

    private byte recordLimitField;

    private SCResponseNotifications notificationsField;

    private byte recordsReturnedField;

    private byte startingRecordField;

    private byte totalRecordsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("link")]
    public SCResponseLink[] link
    {
        get
        {
            return this.linkField;
        }
        set
        {
            this.linkField = value;
        }
    }

    /// <remarks/>
    public byte RecordLimit
    {
        get
        {
            return this.recordLimitField;
        }
        set
        {
            this.recordLimitField = value;
        }
    }

    /// <remarks/>
    public SCResponseNotifications Notifications
    {
        get
        {
            return this.notificationsField;
        }
        set
        {
            this.notificationsField = value;
        }
    }

    /// <remarks/>
    public byte RecordsReturned
    {
        get
        {
            return this.recordsReturnedField;
        }
        set
        {
            this.recordsReturnedField = value;
        }
    }

    /// <remarks/>
    public byte StartingRecord
    {
        get
        {
            return this.startingRecordField;
        }
        set
        {
            this.startingRecordField = value;
        }
    }

    /// <remarks/>
    public byte TotalRecords
    {
        get
        {
            return this.totalRecordsField;
        }
        set
        {
            this.totalRecordsField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class SCResponseLink
{

    private string hrefField;

    private string relField;

    private string titleField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string href
    {
        get
        {
            return this.hrefField;
        }
        set
        {
            this.hrefField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string rel
    {
        get
        {
            return this.relField;
        }
        set
        {
            this.relField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string title
    {
        get
        {
            return this.titleField;
        }
        set
        {
            this.titleField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class SCResponseNotifications
{

    private SCResponseNotificationsNotification notificationField;

    /// <remarks/>
    public SCResponseNotificationsNotification Notification
    {
        get
        {
            return this.notificationField;
        }
        set
        {
            this.notificationField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class SCResponseNotificationsNotification
{

    private System.DateTime notificationDateField;

    private string notificationDetailsField;

    private string statusField;

    private string notificationTitleField;

    private string hrefField;

    /// <remarks/>
    public System.DateTime NotificationDate
    {
        get
        {
            return this.notificationDateField;
        }
        set
        {
            this.notificationDateField = value;
        }
    }

    /// <remarks/>
    public string NotificationDetails
    {
        get
        {
            return this.notificationDetailsField;
        }
        set
        {
            this.notificationDetailsField = value;
        }
    }

    /// <remarks/>
    public string Status
    {
        get
        {
            return this.statusField;
        }
        set
        {
            this.statusField = value;
        }
    }

    /// <remarks/>
    public string NotificationTitle
    {
        get
        {
            return this.notificationTitleField;
        }
        set
        {
            this.notificationTitleField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string href
    {
        get
        {
            return this.hrefField;
        }
        set
        {
            this.hrefField = value;
        }
    }
}


}

问题是,没有任何对象的字段被编组。调用后字段为null或0,即使从WebApi返回的XML肯定具有值。

可能出现什么问题?

感谢。

1 个答案:

答案 0 :(得分:1)

您可能没有在XML格式化程序上打开XmlSerializer。试试这个:

config.Formatters.XmlFormatter.UseXmlSerializer = true;

您还应检查控制器的ModelState属性是否存在可能发生的反序列化错误。