如何使用Linq获取XML数据?

时间:2017-01-20 21:21:25

标签: c# .net xml linq linq-to-xml

我有以下XML。获取数据的最佳方法是什么?

<?xml version='1.0' encoding='UTF-8'?>
<Root>
  <EmployeeDataRoot>
    <EmployeeData>
      <Employee_id>123456</Employee_id>
      <Employee_Status>A</Employee_Status>
      <Business_Unit>EN00</Business_Unit>
      <Cost_Center>0904/1992</Cost_Center>
      <Work_Location>DFW</Work_Location>
      <Location>DFW-HDQ1</Location>
      <Job_Category>0003</Job_Category>
      <Last_Name>John</Last_Name>
      <First_Name>Doe</First_Name>
      <Middle_Name />
      <Preferred_Name />
      <Position_Title>Programmer/Analyst</Position_Title>
      <Legal_Entity>EN00</Legal_Entity>
      <Department_Unit>IT HR &amp; Employee Technology</Department_Unit>
      <Run_Date>2016-12-12</Run_Date>
    </EmployeeData>
  </EmployeeDataRoot>
  <Footer_No_of_Records>
    <Records>1</Records>
  </Footer_No_of_Records>
</Root>

在网上看了一些例子后,我尝试了这两次迭代,但得到了一个错误

  

对象未设置为对象的实例

我查看了Employee课程的属性以及任何拼写错误的节点,但没有看到任何拼写错误。我认为错误是我没有正确查询XML。

var xDoc = XDocument.Load(file.FullName);
listEmployee = 
    (from e in xDoc.Descendants("EmployeeData") 
      select new Employee
      {
        EmployeeID = e.Element("Employee_ID").Value,
        EmployeeStatus = e.Element("Employee_Status").Value,
        BusinessUnit = e.Element("Business_Unit").Value,
        CostCenter = e.Element("Cost_Center").Value,
        WorkLocation = e.Element("Work_Location").Value,
        Location = e.Element("Location").Value,
        JobCategory = e.Element("Job_Category").Value,
        FirstName = e.Element("First_Name").Value,
        LastName = e.Element("Last_Name").Value,
        LegalEntity = e.Element("Legal_Entity").Value
     }
   ).ToList();

我也试过

listEmployee = 
    (from e in xDoc.Element("Root").Elements("EmployeeDataRoot/EmployeeData")
     select new Employee
     {
        EmployeeID = e.Element("Employee_ID").Value,
        EmployeeStatus = e.Element("Employee_Status").Value,
        BusinessUnit = e.Element("Business_Unit").Value,
        CostCenter = e.Element("Cost_Center").Value,
        WorkLocation = e.Element("Work_Location").Value,
        Location = e.Element("Location").Value,
        JobCategory = e.Element("Job_Category").Value,
        FirstName = e.Element("First_Name").Value,
        LastName = e.Element("Last_Name").Value,
        LegalEntity = e.Element("Legal_Entity").Value                                        
    }
  ).ToList();

1 个答案:

答案 0 :(得分:1)

你的尝试,是对的,但你写的“Employee_ID”错了。试试这个:

&#xA;&#xA;
  var xDoc = XDocument.Load(file.FullName);&#xA; listEmployee =&#xA; (来自e在xDoc.Descendants(“EmployeeData”)&#xA;选择新员工&#xA; {&#xA; EmployeeID = e.Element(“Employee_id”)。值,&#xA; EmployeeStatus = e.Element( “Employee_Status”)。值,&#xA; BusinessUnit = e.Element(“Business_Unit”)。值,&#xA; CostCenter = e.Element(“Cost_Center”)。值,&#xA; WorkLocation = e.Element (“Work_Location”)。值,&#xA;位置= e.Element(“位置”)。值,&#xA; JobCategory = e.Element(“Job_Category”)。值,&#xA; FirstName = e。元素(“First_Name”)。值,&#xA; LastName = e.Element(“Last_Name”)。值,&#xA; LegalEntity = e.Element(“Legal_Entity”)。值&#xA;}&#xA; ).ToList();&#XA;  
&#XA;