HOW TO EXTRACT RECORDS OF XML USING C# LINQ?

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
C#  LINQ 
XML
 1240  View(s)
Ratings:
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()

};

  1. Ankita Pandey

    Post:183

    Points:1285
    Re: How to extract records of XML using c# linq?

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

        })

    };

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!