0

这是我在 MVC 中的代码:

    string url = "https://api....";
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    httpWebRequest.Method = "POST";
    httpWebRequest.ContentType = "application/x-www-form-urlencoded";

    string postData = "<?xml version=\"1.0\"?>" +
                        "<request>" +
                            "<login>" + Login + "</login>" +
                            "<password>" + Password + "</password>" +
                            "<limit>" +
                                "<offset>10</offset>" +
                                "<limit>10</limit>" +
                            "</limit>" +
                        "</request>";

    try
    {
        using (StreamWriter requestWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            requestWriter.Write(postData);
        }
    }
    catch (System.Net.WebException ex)
    {
        return null;
    }

    HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    Stream responseStream = httpWebResponse.GetResponseStream();
    StreamReader streamReader = new StreamReader(responseStream);
    var serializer = new System.Xml.Serialization.XmlSerializer(typeof(Class.hotels));
    Class.hotels hotel = (Class.hotels)serializer.Deserialize(streamReader);
    streamReader.Close();
    responseStream.Close();
    httpWebResponse.Close();

我在我的 Windows Phone 7 项目中获得了相同的功能(异步调用的小变化)并且它正在工作。我已将 WP7 项目中的类添加到 MVC,我现在正在 MVC 中尝试。一切正常,但最终我得到了 0 件物品的酒店(但在 WP 中我得到了物品)。我试图读取streamReader字符串,我得到了正确的答案,所以问题一定是反序列化。

那么可能是什么问题?如果类是错误的,我会从 XmlSerializer 收到错误消息,对吗(在 WP 中它也不起作用)?但我不知道还有哪里可能是问题所在。感谢帮助

编辑: 酒店类的一部分:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.17929")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
public partial class hotels : object
{
    private int countfield;
    private ObservableCollection<hotel> hotelfield;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order = 0)]
    public int count
    {
        get
        {
            return this.countfield;
        }
        set
        {
            this.countfield = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order = 1)]
    public ObservableCollection<hotel> hotel
    {
        get
        {
            return this.hotelfield;
        }
        set
        {
            this.hotelfield = value;
        }
    }

}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.17929")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
public partial class hotel : object, System.ComponentModel.INotifyPropertyChanged
{

    private int hotIdField;

    private int hoyIdField;
    ...
    many properties
    ...

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order = 0)]
    public int hotId
    {
        get
        {
            return this.hotIdField;
        }
        set
        {
            this.hotIdField = value;
            this.RaisePropertyChanged("hotId");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order = 1)]
    public int hoyId
    {
        get
        {
            return this.hoyIdField;
        }
        set
        {
            this.hoyIdField = value;
            this.RaisePropertyChanged("hoyId");
        }
    }
    ...
    many getters, setters
    ...

和响应内容(字符串中的xml):

"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hotels>\n    <foundHotels>4655</foundHotels>\n    <hotel>\n        <hotId>12</hotId>\n        <hoyId>1</hoyId>\n        <bookOnline>0</bookOnline>\n        <name>OÁZA Říčany</name>\n        <address>\n            <couId>1</couId>\n            <regId>3</regId>\n            <cotId>51</cotId>\n            <towId>121</towId>\n            <zipId>12093</zipId>\n            <name>OÁZA Říčany</name>\n            <street>V Chobotě 2112</street>\n            <city>Říčany</city>\n            <zip>25101</zip>\n            <country>Česká republika</country>\n            <phone>\n                <number>+420 323 601 170</number>\n                <number>+420 736 679 097</number>\n                <number>724 165 420</number>\n            </phone>\n   ... many properties ...     </hotel>\n    <hotel>\n        <hotId>13</hotId>\n        <hoyId>1</hoyId>\n        <bookOnline>0</bookOnline>\n        <name>Hotel Maxov</name>\n        <address>\n            <couId>1</couId>\n            <regId>14</regId>\n            <cotId>20</cotId>\n            <towId>1317</towId>\n            <zipId>2492</zipId>\n            <name>Hotel Maxov</name>\n            <street>Dolní Maxov 710</street>\n            <city>Josefův Důl</city>\n            <zip>46844</zip>\n            <country>Česká republika</country>\n            <phone>\n                <number>483381085,483381100</number>\n            </phone>\n     ... many properties of another hotel ...        </hotel>\n</hotels>\n"
4

1 回答 1

1

尝试像这样重新定义你的类:

public partial class hotels : object
{
    private int countfield;
    private ObservableCollection<hotel> hotelfield;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order = 0, ElementName="foundHotels")]
    public int count
    {
        get
        {
            return this.countfield;
        }
        set
        {
            this.countfield = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElement(Order = 1,ElementName="hotel")]
    public ObservableCollection<hotel> hotel
    {
        get
        {
            return this.hotelfield;
        }
        set
        {
            this.hotelfield = value;
        }
    }
    // other fields
}

public partial class hotel : object, System.ComponentModel.INotifyPropertyChanged
{
    private int hotIdField;
    private int hoyIdField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order = 0)]
    public int hotId
    {
        get
        {
            return this.hotIdField;
        }
        set
        {
            this.hotIdField = value;
            this.RaisePropertyChanged("hotId");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order = 1)]
    public int hoyId
    {
        get
        {
            return this.hoyIdField;
        }
        set
        {
            this.hoyIdField = value;
            this.RaisePropertyChanged("hoyId");
        }
    }

    // other properties
}
于 2013-01-17T12:34:10.470 回答