簡體   English   中英

使用Linq從XML提取數據

[英]Using Linq to extract data from XML

大家好,我在嘗試使用Linq從以下XML片段中提取數據時遇到了一些麻煩:

<?xml version='1.0' encoding='utf-8'?>
<ReachResponseEnvelope>
  <BPTResponse>
    <QuotePricing xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns:xsd='http://www.w3.org/2001/XMLSchema' CustomerId='2478'
    CustomerQuoteRefNo='' BPTQuoteRefNo='1185129' Product='NNE'
    ResponseCode='P0001' IsQuickQuote='true'
    xmlns='http://www.infoaxon.com/BPT/Services/Schemas/'>
      <TotalPricing>
        <InstallRevenue>2380</InstallRevenue>
        <RentalRevenue>10620</RentalRevenue>
        <AncillaryCharges>
          <Install>0</Install>
          <Rental>0</Rental>
        </AncillaryCharges>
        <QosCost>
          <Install>0</Install>
          <Rental>0</Rental>
        </QosCost>
        <ReportingCost>
          <Install>0</Install>
          <Rental>0</Rental>
        </ReportingCost>
        <TariffSummary>13000</TariffSummary>
      </TotalPricing>
    </QuotePricing>
  </BPTResponse>
</ReachResponseEnvelope>

我具有以下Linq,可以進入QuotoPricing節點,但很難說出InstallRevenue值。 任何幫助將不勝感激!

var v = from page in requestResponses.Elements("ReachResponseEnvelope").Elements("BPTResponse")
select page;

foreach (var record in v)
{
//Struggling here!!!
}

添加XNamespace

 XNamespace ns = "http://www.infoaxon.com/BPT/Services/Schemas/";
 var v = from page in doc.Elements("ReachResponseEnvelope").Elements("BPTResponse")
                    select page;

 foreach (var record in v)
 {
  var installRevenueElement = record.Element(ns+ "QuotePricing").Element(ns+ "TotalPricing").Element(ns + "InstallRevenue");
  Console.WriteLine(installRevenueElement.Value);
 } 

暫無
暫無

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

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