使用linq返回员工到xml

时间:2016-05-21 13:00:13

标签: c# linq-to-xml

鉴于以下xml,我需要返回属于某个部门的所有员工。 因此,当DepartmentName = Fashion应该返回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>

我尝试了但是没有编译,其他尝试没有返回想要的结果

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

2 个答案:

答案 0 :(得分:0)

您可以使用两个from子句a.k.a SelectMany()来过滤Department元素并选择相应的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();

答案 1 :(得分:0)

在你的第一个例子中

Error: ILLEGAL USE OF KEYWORD LIMIT.  TOKEN SKIP OPTIMIZE FOR FETCH , EXCEPT MINUS UNION <END-OF-STATEMENT> WAS EXPECTED. SQLCODE=-199, SQLSTATE=42601, DRIVER=3.59.81
SQLState:  42601
ErrorCode: -199
Error: THE CURSOR SQL_CURLH200C1 IS NOT IN A PREPARED STATE. SQLCODE=-514, SQLSTATE=26501, DRIVER=3.59.81
SQLState:  26501
ErrorCode: -

你做错了是 emp 变量用于 Employees 标记,因此你尝试使用的FirstAttribute和LastAttribute没有任何意义。 请改为使用此代码:

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

我希望它有所帮助。