【问题标题】:How to get data with Linq to XML?如何使用 Linq to XML 获取数据?
【发布时间】:2017-01-20 21:21:24
【问题描述】:

我有以下 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();

【问题讨论】:

  • 我的感觉告诉我e.Element("Employee_ID") 为空,因为元素名称是Employee_id。如果将Element() 调用放在单独的行上而不是将它们嵌入到初始化语法中,这些问题将更容易调试。或添加内联空值检查 (e.Element("First_Name") == null ? "" : e.Element("First_Name").Value)

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


【解决方案1】:

您的尝试是正确的,但您写错了“Employee_ID”。试试这个:

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

【讨论】:

  • 是的,就是这样。我认为这可能是拼写错误的东西并查看了它们,但我当然错过了那个。感谢您发现这一点。
猜你喜欢
  • 1970-01-01
  • 2013-02-22
  • 2013-01-10
  • 1970-01-01
  • 1970-01-01
  • 2011-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多