>我收到一个 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 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# 类
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 转换为列表。我现在在路径中遇到非法错误。任何帮助将不胜感激。谢谢
更新:
这是我在文档的内部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>
试试这个...
public static void ReadInventory()
{
XDocument doc = XDocument.Load(@"D:\Sample\Data2.xml");
XNamespace ns = doc.Root.Name.Namespace; // You will need to use this "ns" in every XPATH except attributes.
var v = from 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 + "rn" );
}
Console.WriteLine(v.Count());
}
我只为开始和结束完成了此操作,但您可以添加其余属性。
如果您有其他问题,请告诉我。
这已经过测试:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication49
{
class Program
{
const string FILENAME = @"c:temptest.xml";
static void Main(string[] args)
{
new Sampleclass(FILENAME);
}
}
public class Sampleclass
{
public static HRootObject hRootObject { get; set; }
public Sampleclass(string filename)
{
XDocument doc = XDocument.Load(filename);
XNamespace ns = ((XElement)doc.FirstNode).GetDefaultNamespace();
hRootObject = doc.Elements(ns + "OTA_HotelInvCountNotifRQ").Select(m => new HRootObject() {
TimeStamp = (DateTime)m.Attribute("TimeStamp"),
Version = (string)m.Attribute("Version"),
Inventories = m.Elements(ns + "Inventories").Select(n => new Inventories() {
HotelCode = (string)n.Attribute("HotelCode"),
Inventory = n.Elements(ns + "Inventory").Select(o => new Inventory() {
StatusApplicationControl = o.Elements(ns + "StatusApplicationControl").Select(p => new StatusApplicationControl() {
Start = (DateTime)p.Attribute("Start"),
End = (DateTime)p.Attribute("End"),
Mon = (int)p.Attribute("Mon"),
Tue = (int)p.Attribute("Tue"),
Wed = (int)p.Attribute("Wed"),
Thur = (int)p.Attribute("Thur"),
Fri = (int)p.Attribute("Fri"),
Sat = (int)p.Attribute("Sat"),
Sun = (int)p.Attribute("Sun"),
InvTypeCode = (string)p.Attribute("InvTypeCode"),
AllInvCode = (Boolean)p.Attribute("AllInvCode")
}).FirstOrDefault(),
InvCounts = o.Elements(ns + "InvCounts").Select(p => new InvCounts() {
InvCount = p.Elements(ns + "InvCount").Select(q => new InvCount() {
Count = (int)q.Attribute("Count"),
CountType = (int)q.Attribute("CountType")
}).FirstOrDefault()
}).FirstOrDefault()
}).ToList()
}).FirstOrDefault()
}).FirstOrDefault();
}
public class StatusApplicationControl
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
public int Mon { get; set; }
public int Tue { get; set; }
public int Wed { get; set; }
public int Thur { get; set; }
public int Fri { get; set; }
public int Sat { get; set; }
public int Sun { get; set; }
public string InvTypeCode { get; set; }
public Boolean AllInvCode { get; set; }
}
public class InvCount
{
public int CountType { get; set; }
public int 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 DateTime TimeStamp { get; set; }
public string Version { get; set; }
public Inventories Inventories { get; set; }
}
}
}
谢谢 jdweng 和 A3006 我已经将您的代码和想法合二为一。如果您希望 xml 格式的 Web 请求,请参阅下面的 Web api 方法
public HttpResponseMessage UpdateHotelAvailability()
{
string incomingText = this.Request.Content.ReadAsStringAsync().Result;
XDocument doc = XDocument.Parse(incomingText);
XNamespace ns = doc.Root.Name.Namespace;
var v = from h in doc.Descendants(ns+"StatusApplicationControl")
select new StatusApplicationControl
{
Start = h.Attribute("Start").Value,
End = h.Attribute("End").Value
};
HttpResponseMessage res = Request.CreateResponse(HttpStatusCode.OK, 200);
return res;
}