forum

home / developersection / forums / how to extract records of xml using c# linq?

How to extract records of XML using c# linq?

Anonymous User 3121 05-Nov-2014

I have the following XML excerpt. I have no problem extracting the first step of XML but I can't figure out how to get to the second layer and extract each layer. Specifically, the user info in XML below.

Any help will be appreciated.

<?xml version="1.0" encoding="UTF-8" ?>
  <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
    <ns2:getStatusReportResponse xmlns:ns2="http://xxxxxxxxx.com/">
      <return>
        <statusReport>
        <record>
          <assessorDate />
          <assessorOffice />
          <availableDate />
          <awardDate>01/01/2014</awardDate>
          <awardValue>1000000</awardValue>
              <businessSector>SYSTEMS</businessSector>
          <user>
            <accessGrantedDate />
            <emailAddress>john.usda@noemail.mil</emailAddress>
            <name>JOHN USDA</name>
            <phoneNumber>XXX-XXX-XXXX</phoneNumber>
            <role>Focal Point</role>
          </user>
          <user>
            <accessGrantedDate />
            <emailAddress>john.usda@noemail.mil</emailAddress>
            <name>JOHN USDA</name>
            <phoneNumber>XXX-XXX-XXXX</phoneNumber>
            <role>Focal Point</role>
          </user>
        </record>
       </statusReport>
      </return>
    </ns2:getStatusReportResponse>
    </S:Body>
  </S:Envelope>

I've tried this but it only get's me a list of the first user record and not all of them.

var records = from x in xml.Descendants("record")
select new
{
      awardDate = (string)x.Descendants("awardDate").FirstOrDefault().Value,
      userList = (List<string>)x.Descendants("user").Elements().Select(a => a.Value).ToList()
};


xml c#  linq 
Updated on 06-Nov-2014

I am a content writter !

Can you answer this question?

Answer

1 Answers

Liked By