2017-04-07 10 views
0

웹 API를 사용하여 C# 목록으로 변환해야하는 xml 형식의 요청을 받고 있습니다. 여기 xml은 웹 API에서 변환을 나열합니다. #

내가 여기
<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> 

내가 요청 XML이 필요

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

는 변환 할 수있는 목록 개체에 대한

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# 클래스 내 C# 방법이다 받도록 요청 XML의 위의 클래스를 사용하여 목록에 추가합니다. 지금 경로에서 불법 오류가 발생했습니다. 모든 도움은 정말 감사하겠습니다. 디. 감사

업데이트 :

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()); 
     } 

는 난 단지 시작에 대한 이런 짓을했는지 ... 내가

<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> 
+0

"경로에 잘못된 경로가 잘못되었습니다"라는 메시지가 표시되면 먼저 경로를 확인해야합니다. 경로를 올바르게 확인할 수있게되면 LINQ to XML을 사용하여 XML 문자열에서 목록을 가져올 수 있습니다. – A3006

+0

@ A3006 어떻게 그럴 수 있니? – Melvin

+0

request.Content.ReadAsStreamAsync()에서 무엇을 얻고 있습니까? 지금 결과가 있습니까? – A3006

답변

1

이를보십시오 문서의 innerxml 노드에서 무엇을 얻을 끝내지 만 나머지 속성을 추가 할 수 있습니다.

추가 질문이있는 경우 알려 주시기 바랍니다.

+0

xml이 파일 형식이지만 동일한 XML이 웹 API에 요청으로 보내지고 읽으려고 할 때 경로 오류로 잘못된 문자가 표시되면 감사합니다 – Melvin

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


} 
+0

답변을 드릴 수있는 시간을 많이 보내 주셔서 감사합니다 로컬 시스템에서 xml 파일 그래서 그들은 불법 문자 경로 error.But 내가 로컬 컴퓨터에서 다음 xml 읽을 경우 그것은 잘 작동하는지 웹 api 요청으로 가져옵니다. – Melvin

+0

문자열 (innerxml)을 가져 오면 .Load (Filename) 대신 .Parse (string)를 사용할 수 있습니다. – jdweng

0

감사 jdweng 및 A3006은 다음 친절

아래의 웹 API 메서드를 참조 XML에서 웹 요청을 기대 끝난 거니 나는 당신의 코드와 아이디어 모두를 결합했다
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; 
    }