LINQ XML - 选择子项具有给定值的所有父元素

时间:2017-07-07 15:48:06

标签: xml vb.net linq linq-to-xml

从下面的XML中,我需要找到有关FlightSegmentReference ref等于BA2759的所有航班的信息,然后将一些细节从父元素传递给一个类

 If Len(inFlightNo) > 0 Then
     Dim xdoc As XDocument = XDocument.Parse(rawXML)
     Dim results As IEnumerable(Of Offer) =
         From offer In xdoc.Descendants(myns + "FlightSegmentReference")
             Where offer.Attribute("ref") = inFlightNo
                 Select New Offer With
                 {.FlightRef = offer.Attribute("ref")}
     'Return results
 End If

这个例子运行正常......但是我需要将更多数据传递给我的类,例如TotalAmount SimpleCurrancyPrice和OfferItemID

 If Len(inFlightNo) > 0 Then
     Dim xdoc As XDocument = XDocument.Parse(rawXML)
     Dim results As IEnumerable(Of Offer) =
         From offer In xdoc.Descendants(myns + "FlightSegmentReference")
             Where offer.Attribute("ref") = inFlightNo
                 Select New Offer With
                 {.FlightRef = offer.Attribute("ref"),
                  .Price = ????,
                  .OfferItemID = ????
                 }
     'Return results
 End If

获取SimpleCurrencyPrice并在我的课程中填充Price的最佳实践方法是什么

这是XML

<AirlineOffers>
   <TotalOfferQuantity>1156</TotalOfferQuantity>
   <Owner>BA</Owner>
   <AirlineOffer RequestedDateInd="true">
      <OfferID Owner="BA">OFFER1</OfferID>
      <TotalPrice>
         <SimpleCurrencyPrice Code="GBP">129.50</SimpleCurrencyPrice>
      </TotalPrice>
      <PricedOffer>
         <OfferPrice OfferItemID="1">
            <RequestedDate>
               <PriceDetail>
                  <TotalAmount>
                     <SimpleCurrencyPrice Code="GBP">129.50</SimpleCurrencyPrice>
                  </TotalAmount>
                  <BaseAmount Code="GBP">84.00</BaseAmount>
                  <Taxes>
                     <Total Code="GBP">45.50</Total>
                  </Taxes>
               </PriceDetail>
               <Associations>
                  <AssociatedTraveler>
                     <TravelerReferences>SH1</TravelerReferences>
                  </AssociatedTraveler>
               </Associations>
            </RequestedDate>
         </OfferPrice>
         <Associations>
            <ApplicableFlight>
               <FlightSegmentReference ref="BA2759">
                  <ClassOfService>
                     <Code>O</Code>
                     <MarketingName>Euro Traveller</MarketingName>
                  </ClassOfService>
               </FlightSegmentReference>
            </ApplicableFlight>
            <PriceClass>
               <PriceClassReference>Plus</PriceClassReference>
            </PriceClass>
         </Associations>
         <Associations>
            <ApplicableFlight>
               <FlightSegmentReference ref="BA2764">
                  <ClassOfService>
                     <Code>Q</Code>
                     <MarketingName>Euro Traveller</MarketingName>
                  </ClassOfService>
               </FlightSegmentReference>
            </ApplicableFlight>
            <PriceClass>
               <PriceClassReference>Plus</PriceClassReference>
            </PriceClass>
         </Associations>
      </PricedOffer>
   </AirlineOffer>
   <AirlineOffer RequestedDateInd="true">
      <OfferID Owner="BA">OFFER2</OfferID>
      <TotalPrice>
         <SimpleCurrencyPrice Code="GBP">129.50</SimpleCurrencyPrice>
      </TotalPrice>
      <PricedOffer>
         <OfferPrice OfferItemID="1">
            <RequestedDate>
               <PriceDetail>
                  <TotalAmount>
                     <SimpleCurrencyPrice Code="GBP">129.50</SimpleCurrencyPrice>
                  </TotalAmount>
                  <BaseAmount Code="GBP">84.00</BaseAmount>
                  <Taxes>
                     <Total Code="GBP">45.50</Total>
                  </Taxes>
               </PriceDetail>
               <Associations>
                  <AssociatedTraveler>
                     <TravelerReferences>SH1</TravelerReferences>
                  </AssociatedTraveler>
               </Associations>
            </RequestedDate>
         </OfferPrice>
         <Associations>
            <ApplicableFlight>
               <FlightSegmentReference ref="BA2765">
                  <ClassOfService>
                     <Code>O</Code>
                     <MarketingName>Euro Traveller</MarketingName>
                  </ClassOfService>
               </FlightSegmentReference>
            </ApplicableFlight>
            <PriceClass>
               <PriceClassReference>Plus</PriceClassReference>
            </PriceClass>
         </Associations>
         <Associations>
            <ApplicableFlight>
               <FlightSegmentReference ref="BA2764">
                  <ClassOfService>
                     <Code>Q</Code>
                     <MarketingName>Euro Traveller</MarketingName>
                  </ClassOfService>
               </FlightSegmentReference>
            </ApplicableFlight>
            <PriceClass>
               <PriceClassReference>Plus</PriceClassReference>
            </PriceClass>
         </Associations>
      </PricedOffer>
   </AirlineOffer>
</AirlineOffers>

2 个答案:

答案 0 :(得分:2)

我不是VB开发人员,但我认为我要提议的很简单,而不是选择select * from articles个节点,选择FlightSegmentReference这是拥有您需要的所有信息的元素,所以它会是:

PricedOffer

答案 1 :(得分:0)

您可以使用Parent从FlightSegmentReference元素导航备份树:

Dim results As IEnumerable(Of Offer) =
    From ref In xdoc.Descendants(myns + "FlightSegmentReference")
    Where ref.Attribute("ref") = inFlightNo
    Let offer = ref.Parent.Parent.Parent.Parent
    Select New Offer With {
        .FlightRef = ref.Attribute("ref"),
        .Price = CDec(offer.Element(myns + "TotalPrice").Element(myns + "SimpleCurrencyPrice").Value),
        .OfferItemID = offer.Element(myns + "PricedOffer").Element(myns + "OfferPrice").Attribute("OfferItemID")
    }

我喜欢使用VB.NET的XML语法支持 - 虽然您需要Import命名空间,但我似乎更具可读性:

Dim results2 As IEnumerable(Of Offer) =
    From e In xdoc...<myns:FlightSegmentReference>
    Where e.@ref = inFlightNo
    Let offer = e.Parent.Parent.Parent.Parent
    Select New Offer() With {
        .FlightRef = e.@ref,
        .Price = CDec(offer.<myns:TotalPrice>.<myns:SimpleCurrencyPrice>.Value),
        .OfferItemID = offer.<myns:PricedOffer>.<myns:OfferPrice>.@OfferItemID
    }