xml列出web api c#

时间:2017-04-07 07:18:18

标签: c# json xml webapi2

我收到了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;
        }

列表对象的#class

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转换为列表。我现在在路径中收到非法错误。任何帮助都会非常感激。 感谢

更新:

这是我在doc

的innerxml节点中得到的
<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>

3 个答案:

答案 0 :(得分:1)

试试这个......

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 + "\r\n" );
            }

            Console.WriteLine(v.Count());
        }

我仅针对“开始”和“结束”执行此操作,但您可以添加其余属性。

如果您有其他问题,请告诉我。

答案 1 :(得分:1)

已经过测试:

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:\temp\test.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; }
        }
    }


}

答案 2 :(得分:0)

感谢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;
    }