簡體   English   中英

Linq-to-xml獲取子節點

[英]Linq-to-xml to get child nodes

我在確定如何使用linq-to-xml來從下面的xml中提取總價格和單個價格時遇到了麻煩(例如,我想獲得票價和所有價格的總和)。 任何幫助將不勝感激,尤其是使用linq-to-xml的方法語法時

我使用以下代碼將數據加載到xDocument中,並與xmlResponse對象一起解析響應。

var xmlResponse = from element in xdoc.Descendants()
                              select element;

並獲得像

xmlResponse.SingleOrDefault(x => x.Name.LocalName == "Registration")

以下是thwe xml響應的子集:

<StateList>
    <State>
      <SourceJobID>J999999999999</SourceJobID>
      <TargetJobState>Complete</TargetJobState>
      <TargetJobID>11111111</TargetJobID>
      <TargetSystem>TESTSYSTEM</TargetSystem>
      <VehicleDetails>
        <Registration>TESTREGISRATION</Registration>
        <Plate>11111111111</Plate>
        <CO2Rating>160</CO2Rating>
        <Badge>1111111</Badge>
        <Description>TEST DESCRIPTION</Description>
      </VehicleDetails>
      <CompleteDetails>
        <CompletedOn>2015-09-15T13:39:11+01:00</CompletedOn>
        <JobDistance>0</JobDistance>
        <WaitingTime />
        <CO2Usage>0</CO2Usage>
        <ChargeList>
          <Charge>
            <Name>Airport Pickup</Name>
            <Currency>GBP</Currency>
            <Price>0.00</Price>
          </Charge>
          <Charge>
            <Name>Fare</Name>
            <Currency>GBP</Currency>
            <Price>0.00</Price>
          </Charge>
          <Charge>
            <Name>Extra Stops</Name>
            <Currency>GBP</Currency>
            <Price>0.00</Price>
          </Charge>
        </ChargeList>
      </CompleteDetails>
    </State>

假設您的示例中只有一個狀態,則可以執行以下操作:

        decimal fare = decimal.Parse(xml.Descendants("Charge").Single(x => x.Element("Name").Value == "Fare").Element("Price").Value);
        decimal total = xml.Descendants("Charge").Sum(x => decimal.Parse(x.Element("Price").Value));

盡管如果列表中有一系列元素,則必須對其進行修改。

編輯:如果,如您在評論中所說,您只想總結某些費用:

        // Valid names of charges to sum.
        string[] names = { "Airport Pickup", "Fare" };
        // Iterate over every state.
        foreach (var state in xml.Descendants("State")) 
        {
            // Get all charge elements in the current state whose names are contained in 'names' - then convert their 'Price' element to decimal and sum them.
            decimal stateTotal = state.Descendants("Charge").Where(x => names.Contains(x.Element("Name").Value)).Sum(x => decimal.Parse(x.Element("Price").Value)); 
        }
if(doc.Descendants("Charge").Any())
{
    var FarePrice = doc.Descendants("Charge")
                .Where(x => x.Descendants("Name").First().Value.Equals("Fare")).First().Element("Price").Value;

    var Sum = doc.Descendants("Charge")
                .Select(x => Convert.ToDouble(x.Descendants("Price").First().Value))
                .Sum();     
    Console.WriteLine("Fare price:{0} Sum:{1}",FarePrice,Sum);
} 

對於10和25輸入,它將返回35作為總和。

在這里提琴: https : //dotnetfiddle.net/cuHXBn

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM