2017-07-07 84 views
0

從下面的XML中,我需要找到有關FlightSegmentReference ref等於BA2759的所有航班的信息,然後將某些細節從父元素傳遞到類LINQ XML - 選擇兒童具有給定值的所有父元素

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 

這個例子正常工作......不過,我需要得到更多的數據傳遞給我的課,如總金額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和填充價格的最佳實踐方法在我的班級

這裏是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> 
+0

價格和OfferID與所選航段的關係如何?價格代碼似乎在相同的 elelment中,但我沒有看到OfferId是如何相關的。 –

回答

2

我不是一個VB開發人員,但我認爲這是很容易我向你求婚,而不是選擇FlightSegmentReference節點,選擇PricedOffer這是有你需要的所有信息元素,所以這將是:

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

這似乎是更好的方法。 – Mark

+0

非常感謝,不僅工作,而且我也瞭解它。 –

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 [email protected] = inFlightNo 
    Let offer = e.Parent.Parent.Parent.Parent 
    Select New Offer() With { 
     .FlightRef = [email protected], 
     .Price = CDec(offer.<myns:TotalPrice>.<myns:SimpleCurrencyPrice>.Value), 
     .OfferItemID = offer.<myns:PricedOffer>.<myns:OfferPrice>[email protected] 
    } 
相關問題