ASP.NET MVC -反序列化来自SOAP web服务的响应
本文关键字:web SOAP 服务 响应 NET MVC 反序列化 ASP | 更新日期: 2023-09-27 17:54:19
这是我在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 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"
试着像这样重新定义你的类:
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
}