我得到一個xml的形式的請求,我需要在c#中使用web api轉換爲列表。xml到web api中的列表轉換c#
下面是請求XML,我收到
<OTA_HotelInvCountNotifRQ xmlns="http://www.zzz.com/OTA/2015/03"
TimeStamp="2015-03-10T09:41:51.982" Version="1.2"> <Inventories
HotelCode="10001"> <Inventory> <StatusApplicationControl Start="2015-03-16"
End="2015-03-30" Mon="0" Tue="0" Wed="0" Thur="0" Fri="0" Sat="1" Sun="1"
InvTypeCode="DLX" AllInvCode="False" /> <InvCounts> <InvCount CountType="2"
Count="17" /> </InvCounts> </Inventory> <Inventory>
<StatusApplicationControl Start="2015-03-16" End="2015-03-30"
AllInvCode="False" Mon="1" Tue="1" Wed="1" Thur="1" Fri="1" Sat="1" Sun="1"
InvTypeCode="STD"></StatusApplicationControl> <InvCounts> <InvCount
CountType="2" Count="7" /> </InvCounts> </Inventory> </Inventories>
</OTA_HotelInvCountNotifRQ>
這裏是我的C#方法列表對象
public class sampleclass
{
public class StatusApplicationControl
{
public string Start { get; set; }
public string End { get; set; }
public string Mon { get; set; }
public string Tue { get; set; }
public string Wed { get; set; }
public string Thur { get; set; }
public string Fri { get; set; }
public string Sat { get; set; }
public string Sun { get; set; }
public string InvTypeCode { get; set; }
public string AllInvCode { get; set; }
}
public class InvCount
{
public string CountType { get; set; }
public string Count { get; set; }
}
public class InvCounts
{
public InvCount InvCount { get; set; }
}
public class Inventory
{
public StatusApplicationControl StatusApplicationControl { get; set; }
public InvCounts InvCounts { get; set; }
}
public class Inventories
{
public string HotelCode { get; set; }
public List<Inventory> Inventory { get; set; }
}
public class HRootObject
{
public string TimeStamp { get; set; }
public string Version { get; set; }
public Inventories Inventories { get; set; }
}
}
我需要請求XML轉換
public HttpResponseMessage UpdateHotelAvailability(HttpRequestMessage request)
{
var doc = new XmlDocument();
doc.Load(request.Content.ReadAsStreamAsync().Result);
var serializer = new XmlSerializer(typeof(HRootObject));
using (var reader = XmlReader.Create(doc.InnerXml))
{
HRootObject Hobj = (HRootObject)serializer.Deserialize(reader);
}
HttpResponseMessage res = Request.CreateResponse(HttpStatusCode.OK, 200);
return res;
}
C#類通過使用上面的類到一個列表。我現在得到非法錯誤的路徑。任何幫助將非常感激d。 感謝
更新:
這是我在doc
<OTA_HotelInvCountNotifRQ xmlns="http://www.zzz.com/OTA/2015/03"
TimeStamp="2015-03-10T09:41:51.982" Version="1.2"> <Inventories
HotelCode="10001"> <Inventory> <StatusApplicationControl Start="2015-03-16"
End="2015-03-30" Mon="0" Tue="0" Wed="0" Thur="0" Fri="0" Sat="1" Sun="1"
InvTypeCode="DLX" AllInvCode="False" /> <InvCounts> <InvCount CountType="2"
Count="17" /> </InvCounts> </Inventory> <Inventory>
<StatusApplicationControl Start="2015-03-16" End="2015-03-30"
AllInvCode="False" Mon="1" Tue="1" Wed="1" Thur="1" Fri="1" Sat="1" Sun="1"
InvTypeCode="STD"></StatusApplicationControl> <InvCounts> <InvCount
CountType="2" Count="7" /> </InvCounts> </Inventory> </Inventories>
</OTA_HotelInvCountNotifRQ>
如果您收到「路徑中的非法錯誤」,您必須先檢查路徑。一旦你可以正確解析路徑,你可以使用LINQ to XML從XML字符串中獲取列表。 – A3006
@ A3006任何想法如何做到這一點? – Melvin
你在request.Content.ReadAsStreamAsync()中得到什麼結果? – A3006