我已使用这些XSDs的xsd工具生成了类。但是在向api发出请求之后,响应消息说我没有发送属性flexibleAllocation,当我明确这样做时。
序列化程序必定有问题,但它不会抛出任何execptions。
这就是我做过的事情:
public AvailRateUpdateRS AvailRateUpdate()
{
var refDate = DateTime.Now;
var requestObject = new AvailRateUpdateRQ()
{
Authentication =
new AvailRateUpdateRQAuthentication()
{
username = "SomeUsername",
password = "SomePassword"
},
Hotel = new AvailRateUpdateRQHotel()
{
id = 12333123
},
Items = new[] {
new AvailRateUpdateRQAvailRateUpdate
{
DateRange = new DateRangeType[]{
new DateRangeType{
from = refDate,
to = refDate
}
},
RoomType = new RoomTypeType[]{
new RoomTypeType{
id = "123313",
Inventory = new RoomTypeTypeInventory{
flexibleAllocation = 3,
}
}
}
}
}
};
var httpRequest = HttpWebRequest.Create("https://services.expediapartnercentral.com/eqc/ar");
httpRequest.Method = "POST";
httpRequest.ContentType = "text/xml";
var requestXml = new XmlDocument();
using (var requestStream = httpRequest.GetRequestStream())
{
var serializer = new XmlSerializer(typeof(AvailRateUpdateRQ));
serializer.Serialize(requestStream, requestObject);
requestXml.Save(requestStream);
}
using (var response = (HttpWebResponse)httpRequest.GetResponse())
using (var responseStream = response.GetResponseStream())
{
XmlSerializer serializer = new XmlSerializer(typeof(AvailRateUpdateRS));
AvailRateUpdateRS deserialized = (AvailRateUpdateRS)serializer.Deserialize(responseStream);
return deserialized;
}
}
为什么在序列化时会跳过flexibleAllocation属性?
序列化消息:
<?xml version="1.0" encoding="utf-8"?>
<AvailRateUpdateRQ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.expediaconnect.com/EQC/AR/2011/06">
<Authentication username="SomeUsername" password="SomePassword" />
<Hotel id="12333123" />
<AvailRateUpdate>
<DateRange from="2018-03-07" to="2018-03-07" />
<RoomType id="123313">
<Inventory />
</RoomType>
</AvailRateUpdate>
</AvailRateUpdateRQ>
BTW类(AvailRateUpdateRQ和其他)是使用xsd文件中的xsd工具生成的,并且没有以任何其他方式进行修改。
答案 0 :(得分:0)
因为这些属性是可选的,如果没有明确指定将不会被序列化。
示例:虽然这个
Inventory = new RoomTypeTypeInventory{
flexibleAllocation = 3,
}
将被序列化为<Inventory />
,但会添加其他属性
Inventory = new RoomTypeTypeInventory{
flexibleAllocation = 3,
flexibleAllocationSpecified = true
}
将导致所需的序列化行为<Inventory flexibleAllocation="3" />
这适用于Expedia API架构文件中生成的类。