json - xml to list conversion in web api c# -
i getting request in form of xml need converted list in c# using web api.
here's request xml receive
<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>
here's c# method
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# class list object
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; } } }
i need request xml converted list using above class.i getting illegal error in path now.any appreciated. thanks
update :
this in innerxml node of 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>
try this...
public static void readinventory() { xdocument doc = xdocument.load(@"d:\\sample\\data2.xml"); xnamespace ns = doc.root.name.namespace; // need use "ns" in every xpath except attributes. var v = h in doc.descendants(ns+"statusapplicationcontrol") select new statusapplicationcontrol { start = h.attribute("start").value, end = h.attribute("end").value }; foreach (var item in v) { console.write("start" + item.start + " end " + item.end + "\r\n" ); } console.writeline(v.count()); }
i have done start , end, can add remaining attributes.
let me know in case have further questions.
Comments
Post a Comment