2012-02-17 114 views
1

提取数据,大家好我挣扎了一下,试图使用LINQ从下面这段XML的提取数据:使用LINQ to 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!!! 
} 
+0

你可以从[这里]一些想法[1]? [1]:http://stackoverflow.com/questions/670563/linq-to-read-xml – Junaid 2012-02-17 14:56:25

回答

1

添加的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); 
} 
+0

这个作品真的很好,你能解释一下添加的命名空间不好吗? – 2012-02-17 15:14:47

+0

@ VinceAshby-Smith QuotePricing,TotalPricing等元素在名称空间中。欲了解更多信息 - http://msdn.microsoft.com/en-us/library/system.xml.linq.xnamespace.aspx和http://msdn.microsoft.com/en-us/library/bb387075.aspx – adatapost 2012-02-17 15:19:58

+0

非常感谢:) – 2012-02-17 15:22:10