Home > DeveloperSection > Forums > How to extract records of XML using c# linq?
Ankit Singh

Total Post:341

Points:2389
Posted on    November-05-2014 10:37 PM

 XML C#  LINQ 
Ratings:


 1 Reply(s)
 879  View(s)
Rate this:

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()

};



Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    November-06-2014 12:44 AM

I am assuming this is the model of your User class:

public class User

{

    public DateTime? AccessGrantedDate { get; set; }

    public string EMailAddress { get; set; }

    public string Name { get; set; }

    public string PhoneNumber { get; set; }

    public string Role { get; set; }

}

And this is extracting the User class from the XML:

var records = from x in xml.Descendants("record")

select new

{

      AwardDate = (string)x.Element("awardDate"),

      UserList = x.Descendants("user").Select(user => new User

      {

          AccessGrantedDate = string.IsNullOrEmpty((string)user.Element("accessGrantedDate")) ?

                                        (DateTime?)null :                      DateTime.Parse((string)user.Element("accessGrantedDate")),

           EMailAddress = (string)user.Element("emailAddress"),

           Name = (string)user.Element("name"),

           PhoneNumber = (string)user.Element("phoneNumber"),

           Role = (string)user.Element("role")

    })

};


Don't want to miss updates? Please click the below button!

Follow MindStick