---
title: "How to extract records of XML using c# linq?"  
description: "How to extract records of XML using c# linq?"  
author: "Anonymous User"  
published: 2014-11-05  
updated: 2014-11-06  
canonical: https://www.mindstick.com/forum/2522/how-to-extract-records-of-xml-using-c-sharp-linq  
category: "xml"  
tags: ["c#", "linq"]  
reading_time: 2 minutes  

---

# How to extract records of XML using c# linq?

I have the following [XML](https://www.mindstick.com/blog/203/working-with-xml-data-in-sql-server) excerpt. I have no [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) extracting the first step of XML but I can't [figure](https://yourviews.mindstick.com/view/87536/what-are-ghosts-jobs-and-how-to-figure-out-them) out how to get to the [second](https://answers.mindstick.com/qa/41761/how-did-the-second-industrial-revolution-influence-women-s-roles-in-society) [layer](https://answers.mindstick.com/qa/116083/give-two-examples-of-protocols-that-operate-at-the-application-layer) and [extract](https://www.mindstick.com/forum/12883/how-to-extract-sub-string-between-two-string-pattern-using-javascript-jquery) each layer. Specifically, the [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) [info](https://answers.mindstick.com/qa/93284/what-can-we-write-in-about-us-column-in-basic-info-profile) 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()};
```

## Replies

### Reply by Anonymous User

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")    })};
```


---

Original Source: https://www.mindstick.com/forum/2522/how-to-extract-records-of-xml-using-c-sharp-linq

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
