Given the below xml I need to return all the employees that belong to a department. So when DepartmentName=Fashion should return 3 employees
<?xml version="1.0" encoding="utf-8" ?>
<Store>
<Departments>
<Department name="Fashion">
<Employees>
<Employee FirstName="Jo" Surname="Blogg"/>
<Employee FirstName="Mark" Surname="Smith"/>
<Employee FirstName="Rose" Surname="Blogg2"/>
</Employees>
</Department>
<Department name="Makeup">
<Employees>
<Employee FirstName="Sonia" Surname="Smith2"/>
<Employee FirstName="Jenny" Surname="Blogg3"/>
</Employees>
</Department>
</Departments>
</Store>
what I have tried but does not compile and other tries didnt return the wanted result
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace ConsoleApplicationXml
{
class Program
{
static void Main(string[] args)
{
var xDocument = XDocument.Load("Store.xml");
//Get all employees that belong to "Fashion
string departmentName = "Fashion";
//compiles but get object variable not set
var employees = (from emp in xDocument.Descendants("Employees")
where (emp.Parent.FirstAttribute.Value == departmentName)
select new Employee
{
DepartmentName = departmentName,
FirstName = emp.FirstAttribute.Value,
Surname = emp.LastAttribute.Value
}).ToList();
//DOES NOT COMPILE!!
var employees = (from emp in xDocument.Descendants("Employees")
where (emp.Parent.FirstAttribute.Value == departmentName)
let xFirstName = emp.Element("Employee").FirstAttribute("FirstName")
let xLastName = emp.Element("LastName")
select new Employee
{
DepartmentName = departmentName,
FirstName = xFirstName.Value,
Surname = xLastName.Value
}).ToList();
}
}
public class Employee
{
public string DepartmentName { get; set; }
public string FirstName { get; set; }
public string Surname { get; set; }
}
}