XML对LINQ的响应



我的XML响应看起来像:

"<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<HotelAvailResponse xmlns="http://www.juniper.es/webservice/2007/">
<AvailabilityRS Url="http://xml.bookingengine.es"
TimeStamp="20130327T14:38:54.6916528+01:00"
IntCode="lCf65bPrG+x7VDLB0IquVNQgKloRA9+HOuhfHMj0BcE=">
<Results>
<HotelResult Code="7b0LYEzfsd0HH90sd" JPCode="JP003600" DestinationZone="39303" JPDCode="JPD000014">
<HotelInfo>
<Name>Hotel Test</Name>
<Description>A spacious, quiet, light and bright hotel, with a garden and fabulous views across the city.</Description>
<Images>
<Image>http://www.bookingengine.es/images/upload_p/hoteltest.jpeg</Image>
</Images>
<HotelCategory>1 1/2 Stars</HotelCategory>
<HotelType Type="SKI">Snow</HotelType>
<Address>c/tap</Address>
</HotelInfo>
<HotelOptions>
<HotelOption RatePlanCode="dcFZbKty1cJGKeRtgxIDGUZAprp1mua8ySl4iVIZ7NVKBF/PGk8lhZlN7Hcszjs2RwUR2Dxsrv5l0cZDORKz6frEmPdibqOyV2Jg4Dxz8/bF5gqPyQR8+z1LEu8LCVlS" Status="OK">
<Board Type="AD">Bed&amp;Breakfast</Board>
<Prices>
<Price Type="S" Currency="USD">
<TotalFixAmounts Gross="353.65" Nett="353.65">
<Service Amount="321.5"/>
<ServiceTaxes Included="true" Amount="32.15"/>
<Commissions Included="true" Amount="0"/>
<HandlingFees Included="true" Amount="0"/>
<Discount Amount="-0"/>
</TotalFixAmounts>
</Price>
</Prices>
<HotelRooms>
<HotelRoom Units="1" Source="1" AvailRooms="12">
<Name>Double Room</Name>
<RoomCategory Type="DBL">Double Standard</RoomCategory>
</HotelRoom>
<HotelRoom Units="1" Source="2" AvailRooms="45">
<Name>Single</Name>
<RoomCategory Type="SGL">Single Standard</RoomCategory>
</HotelRoom>
</HotelRooms>
</HotelOption>
<HotelOption RatePlanCode="dcFZbKty1cJGKeRtgxIDGUZAprp1mua8ySl4iVIZ7NVKBF/PGk8lhZlN7Hcszjs2RwUR2Dxsrv5l0cZDORKz6frEmPdibqOyV2Jg4Dxz8/bmoX041DU9+3D3nHCEB/6vYKbVtJR2qaHwW9VnnWl/KA==" Status="OK">
<Board Type="AD">Bed&amp;Breakfast</Board>
<Prices>
<Price Type="S" Currency="USD">
<TotalFixAmounts Gross="353.65" Nett="353.65">
<Service Amount="321.5"/>
<ServiceTaxes Included="true" Amount="32.15"/>
<Commissions Included="true" Amount="0"/>
<HandlingFees Included="true" Amount="0"/>
<Discount Amount="-0"/>
</TotalFixAmounts>
</Price>
</Prices>
<HotelRooms>
<HotelRoom Units="1" Source="1" AvailRooms="12">
<Name>Double Room</Name>
<RoomCategory Type="DBL">Double Standard</RoomCategory>
</HotelRoom>
<HotelRoom Units="1" Source="2" AvailRooms="11">
<Name>Double Room</Name>
<RoomCategory Type="DBL">Double Standard</RoomCategory>
</HotelRoom>
</HotelRooms>
<AdditionalElements>
<HotelOffers>
<HotelOffer>
<Name>Basic Discount 10%</Name>
</HotelOffer>
</HotelOffers>
</AdditionalElements>
</HotelOption>
</HotelOptions>
</HotelResult>
</Results>
</AvailabilityRS>
</HotelAvailResponse>
</soap:Body>
</soap:Envelope>"

,我有此linq的响应,如下所示:

XNamespace ns = "http://schemas.xmlsoap.org/soap/envelope/";
var hotels = (from hotelData in data.Descendants(ns + "Envelope").Descendants(ns + "Body").Descendants("HotelAvailResponse").Descendants("HotelAvailResult").Descendants("Results").Descendants("HotelResult")
                      select new Hotel
                          {
                              Code = hotelData.Attribute("Code").Value,
                              JpCode =
                                  hotelData.Attributes().Any(x => x.Name == "JPCode")
                                      ? hotelData.Attribute("JPCode").Value
                                      : "",
                              DestinationZone =
                                  hotelData.Attribute("DestinationZone") != null
                                      ? hotelData.Attribute("DestinationZone").Value
                                      : string.Empty,
                              JpdCode = hotelData.Attribute("JPDCode").Value,
                              //HotelName = Convert.ToString(hotelData.Element("Item").Value),
                              //Rating = Convert.ToInt32(hotelData.Element("StarRating").Value),
                              HotelInfo = (from hi in hotelData.Descendants("HotelInfo")
                              select new HotelInfo
                                  {
                                      Name = hi.Element("Name").Value,
                                      Description = hi.Element("Description").Value,
                                      Image = (from img in hi.Descendants("Images") select new Imagez { Images = img.Element("Image").Value }).ToList(),
                                      HotelCategory = hi.Element("Name").Value,
                                      HotelType = hi.Element("Description").Value,
                                      Address = hi.Element("Description").Value,
                                  }
                                  ).ToList(),
                              HotelOptions = (from ho in hotelData.Descendants("HotelOptions")
                                                  select new HotelOptions()
                                                      {
                                                          HotelOption = ho.Element("HotelOption").Attribute("RatePlanCode").Value,
                                                          Board = ho.Element("Board").Attribute("Type").Value,
                                                          Prices = (from pr in ho.Descendants("Prices") select new Prices() { Price = pr.Element("Price").Value,
                                                          TotalFixAmounts = (from tfa in pr.Descendants("TotalFixAmounts") select new TotalFixAmounts() 
                                                          { Service = tfa.Element("Service").Attribute("Amount").Value,
                                                            ServiceTaxes = tfa.Element("ServiceTaxes").Attribute("Included").Value,
                                                            AmountServiceTaxes = tfa.Element("ServiceTaxes").Attribute("Amount").Value,
                                                            Commissions = tfa.Element("Commissions").Attribute("Included").Value,
                                                            AmountCommissions = tfa.Element("Commissions").Attribute("Amount").Value,
                                                            HandlingFees = tfa.Element("HandlingFees").Attribute("Included").Value,
                                                            AmountHandlingFees = tfa.Element("HandlingFees").Attribute("Amount").Value,
                                                            Discount = tfa.Element("Amount").Attribute("Included").Value,
                                                          }).ToList(),
                                                          }).ToList(),
                                                      }).ToList(),
                          }).ToList();
        return hotels;

我没有任何错误,也没有任何例外,但是返回的酒店计数为0。我是Linq的初学者。任何帮助都将不胜感激,并且连续7个小时,我正在寻找帮助并尝试完成帮助。现在我感到死胡同。

预先感谢。

您已发布的代码有一些问题。

第一个问题是代码未解释分配给<HotelResult>的命名空间。名称空间是http://wwww.juniper.es/webservice/2007/,并从<HotelAvailResponse>元素继承。您可以将名称空间视为xmlns属性:

<HotelAvailResponse xmlns="http://www.juniper.es/webservice/2007/">

这是您的LINQ查询没有返回任何内容的原因 - 它正在寻找使用XML名称空间http://schemas.xmlsoap.org/soap/soap/envelope的<HotelAvailResponse>,并且该节点不存在,因此您可以获得一个空的集合。每次呼叫DescendantsElement时,都需要包括命名空间(即ns + "ElementName")。

第二期直到代码无视运行,但是以下语句

不容易出现。
HotelOptions = (from ho in hotelData.Descendants(ns + "HotelOption")

将导致<HotelOption><Board>仅出现一次(即1个列表)而不是两次。<price>信息和<TotalFixAmounts>正确填充了 - 我不完全确定为什么,但这可能与<TotalFixAmounts>的嵌套列表有关。通过将选择更改为<HotelOption>,很容易解决这:

HotelOption = (from ho in hotelData.Descendants("HotelOption")

现在ho将是<HotelOption>节点及其子女的集合,也将处理嵌套列表。

接下来,您的LINQ语句有两个问题,这些问题将引发NULL引用异常(假设命名空间问题已纠正)。这些是:

Board = ho.Element("Board").Attribute("Type").Value;

ho是所有<HotelOptions>节点及其子女的集合 - 但是<Board><HotelOption>的孩子,本身就是<HotelOptions>的孩子。使用XML时,请记住它本质上是层次结构-.Element(elementName)将访问父母元素的孩子(不是一个孩子(不是大孩子)或较远的孩子)的 first 元素。一个相当简单的解决方案是将<HotelOption>添加到语句中:

Board = ho.Element(ns + "HotelOption").Element(ns + "Board").Attribute("Type").Value;

这里发生了类似的问题:

TotalFixAmounts = (from tfa in pr.Descendants("Prices") select new

pr<Prices>节点的集合,但是select语句中引用的元素是<Price>的子女,而不是<Prices>Element(elementName)将获得父元素的 first <Prices><Price>外没有任何孩子。

最后,没有<Amount>元素是<TotalFixAmounts>的孩子,因此以下行还会引发null参考例外:

Discount = tfa.Element("Amount").Attribute("Included").Value;

代替您使用的两个三元运算符,我建议使用(string)-显式铸件将安全处理缺失的元素或属性。如果缺少元素或属性,则代码不会失败,它将仅对该属性具有任何值。

所以,将所有内容放在一起,您可以得到:

XNamespace ns = "http://www.juniper.es/webservice/2007/";
var hotels = (from hotelData in data.Root.Descendants(ns + "HotelResult")
              select new Hotel
              {
                  Code = (string)hotelData.Attribute("Code"),
                  JpCode = (string)hotelData.Attribute("JPCode"),
                  DestinationZone = (string)hotelData.Attribute("DestinationZone"),
                  JpdCode = (string)hotelData.Attribute("JPDCode"),
                  HotelInfo = (from hi in hotelData.Descendants(ns + "HotelInfo")
                               select new HotelInfo
                               {
                                   Name = (string)hi.Element("Name"),
                                   Description = (string)hi.Element(ns + "Description"),
                                   Image = (from img in hi.Descendants(ns + "Images")
                                            select new Imagez
                                            {
                                                Images = (string)img.Element(ns + "Image")
                                            }).ToList(),
                                   HotelCategory = (string)hi.Element(ns + "Name"),
                                   HotelType = (string)hi.Element(ns + "Description"),
                                   Address = (string)hi.Element(ns + "Description"),
                               }).ToList(),
                  HotelOptions = (from ho in hotelData.Descendants(ns + "HotelOption")
                                  select new HotelOptions
                                  {
                                      HotelOption = ho.Attribute("RatePlanCode").Value,
                                      Board = ho.Element(ns + "Board").Attribute("Type").Value,
                                      Prices = (from pr in ho.Descendants(ns + "Prices")
                                                select new Price
                                                {
                                                    Price = (string)pr.Element(ns + "Price"),
                                                    TotalFixAmounts = (from tfa in pr.Descendants(ns + "Price").Descendants(ns + "TotalFixAmounts")
                                                                       select new TotalFixAmounts
                                                                       {
                                                                           Service = tfa.Element(ns + "Service").Attribute("Amount").Value,
                                                                           ServiceTaxes = tfa.Element(ns + "ServiceTaxes").Attribute("Included").Value,
                                                                           AmountServiceTaxes = tfa.Element(ns + "ServiceTaxes").Attribute("Amount").Value,
                                                                           Commissions = tfa.Element(ns + "Commissions").Attribute("Included").Value,
                                                                           AmountCommissions = tfa.Element(ns + "Commissions").Attribute("Amount").Value,
                                                                           HandlingFees = tfa.Element(ns + "HandlingFees").Attribute("Included").Value,
                                                                           AmountHandlingFees = tfa.Element(ns + "HandlingFees").Attribute("Amount").Value
                                                                       }).ToList(),
                                                }).ToList(),
                                  }).ToList(),
              }).ToList();

请注意,每个引用DescendantsElement都有名称空间ns

最新更新