2017-07-07 4 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 

この例では、正常に動作します...しかし、私は、このような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を取得し、価格を取り込むのベストプラクティス方法をあります私のクラスではここで はあなたがFlightSegmentReference要素からツリーをバックアップナビゲートするParentを使用することができます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

価格とオファーIDは、選択したフライトセグメントにどのように関連していますか?価格コードは同じ elelmentにあるようですが、OfferIdがどのように関連しているのか分かりません。 –

答えて

2

私はVBの開発者ではないが、私が思うには、あなたが必要とするすべての情報を持っている要素であるPricedOfferを選択し、代わりに選択FlightSegmentReferenceノードで、私が提案しようとして何に簡単ですので、それは次のようになります。

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

です:私はVB.NETのXML構文のサポートを使用して好き

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") 
    } 

- それはあなたが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] 
    } 
する必要がありますが、私にはより読みやすいようです
関連する問題