return 名员工使用 linq to xml

return employees using linq to xml

鉴于以下 xml 我需要 return 属于一个部门的所有员工。 所以当 DepartmentName=Fashion 应该 return 3 名员工

    <?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>

我已经尝试但无法编译,其他尝试也没有 return 想要的结果

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

//不编译!!

                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; }
        }
    }

可以使用两个from子句a.k.aSelectMany()来过滤Department个元素和select对应的Employee个元素:

var employees = (from department in xDocument.Descendants("Department")
                 from emp in department.Descendants("Employee")
                 where department.Attribute("name").Value == departmentName
                 select new Employee
                 {
                    DepartmentName = departmentName,
                    FirstName = emp.Attribute("FirstName").Value,
                    Surname = emp.Attribute("Surname").Value
                 }).ToList();

在你的第一个例子中

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

您做错的是 emp 变量是针对 Employees 标签的,因此您尝试使用 FirstAttribute 和 LastAttribute使用没有任何意义。 请改用此代码:

var employeesParent = xDocument.Descendants("Employees").Where(element => element.Parent.Attribute("name").Value == departmentName);
var employees = (from emp in employeesParent.Descendants()
                         select new Employee
                         {
                             DepartmentName = departmentName,
                             FirstName = emp.Attribute("FirstName").Value,
                             Surname = emp.Attribute("Surname").Value
                         }).ToList();

希望对您有所帮助。