ASP.NET MVC - 反序列化SOAP Web服务的响应

时间:2013-01-17 09:11:54

标签: c# asp.net-mvc serialization soap xml-serialization

这是我在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"

1 个答案:

答案 0 :(得分: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
}