Vince Ashby-Smith
Vince Ashby-Smith

Reputation: 1192

Using Linq to extract data from XML

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!!!
}

Upvotes: 1

Views: 806

Answers (1)

KV Prajapati
KV Prajapati

Reputation: 94645

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

Upvotes: 1

Related Questions