0

This is my code in 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();

I got same functionality in my Windows Phone 7 project (little changes with asynchronous calling) and it's working. I have added classes from WP7 project to MVC and I am trying now in MVC. Everything is working but in final I got hotel with 0 items (but in WP I got items in there). I tried to read streamReader to string and I got there right answer so the problem must be with deserializing.

So what could be problem? If class would be wrong I get error message from XmlSerializer, am I right (and in WP it wouldn't be working too)? But I don't know where else could be the problem. Thanks for help

Edit: Part of hotels class:

[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
    ...

and content of response (xml in string):

"<?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"
6
  • Have you checked whether streamReader.ReadToEnd() returns valid data? Commented Jan 17, 2013 at 9:34
  • Yea, that is what I mean whit this: I tried to read streamReader to string and I got there right answer so the problem must be with deserializing. Commented Jan 17, 2013 at 9:38
  • Could you also add a sample of the XML response and the Class.hotel definition? Commented Jan 17, 2013 at 10:56
  • No problem with showing. It's just many information in it. Class.hotel and Class.hotels is automatic generated from web service. Commented Jan 17, 2013 at 11:34
  • @Bibo does your hotel class happen to have any readonly properties that your trying to deserialize? Commented Jan 17, 2013 at 11:37

1 Answer 1

1

Try redefining your classes like this:

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.