1

Hi all i'm struggling a bit with trying to use Linq to extract data from the following piece of 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>

I've got the following piece of Linq, i can get into the QuotoPricing node but struggling to get say the InstallRevenue value. Any help would be hugely appreciated!!

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

foreach (var record in v)
{
//Struggling here!!!
}
Vince Ashby-Smith
  • 1,152
  • 4
  • 18
  • 36
  • can you get some idea from [here][1]? [1]: http://stackoverflow.com/questions/670563/linq-to-read-xml – Junaid Feb 17 '12 at 14:56

1 Answers1

1

Add 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);
 } 
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • This works really well, can you explain what adding the namespace does please? – Vince Ashby-Smith Feb 17 '12 at 15:14
  • @VinceAshby-Smith QuotePricing, TotalPricing etc elements are in the namespace. For more info - http://msdn.microsoft.com/en-us/library/system.xml.linq.xnamespace.aspx and http://msdn.microsoft.com/en-us/library/bb387075.aspx – KV Prajapati Feb 17 '12 at 15:19