如何使用 Linq 获取数据到 XML?
How to get data with 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 & 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>
网上看了一些例子,试了两次都报错
object not set to an instance of an object
我查看了 Employee
class 的属性以及节点是否有任何拼写错误,但没有发现任何拼写错误。我认为错误是我没有正确查询 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();
你的尝试是对的,但是你"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();
我有以下 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 & 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>
网上看了一些例子,试了两次都报错
object not set to an instance of an object
我查看了 Employee
class 的属性以及节点是否有任何拼写错误,但没有发现任何拼写错误。我认为错误是我没有正确查询 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();
你的尝试是对的,但是你"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();