0

我有一些 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>

'Paste as Special' 自动生成的类如下所示:

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


}

问题是,没有一个对象的字段被编组。即使从 WebApi 返回的 XML 肯定有值,调用后字段为空或为零。

可能出了什么问题?

谢谢。

4

1 回答 1

1

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

config.Formatters.XmlFormatter.UseXmlSerializer = true;

您还应该检查控制器的 ModelState 属性以了解可能发生的任何反序列化错误。

于 2013-03-19T15:59:25.160 回答