简体   繁体   English

使用LINQ解析复杂的XML

[英]Parsing complex XML with LINQ

I have the following XML that I have generated: 我已经生成了以下XML:

<?xml version="1.0" encoding="utf-8"?>
<Employees>
  <Employee>
    <First_Name>John</First_Name>
    <Last_Name>Doe</Last_Name>
    <TSR>12345</TSR>
    <Assignments>
      <Assignment>
        <Division>California</Division>
        <Project>Sales</Project>
        <Title>Agent</Title>
        <Start_Date PartTime="False">6/13/2012</Start_Date>
        <Supervisor>Jack Moore</Supervisor>
        <Trainer></Trainer>
        <End_Date TrainingNoShow="False">3/1/2016</End_Date>
        <Separation_Reason>Job was not a fit</Separation_Reason>
        <Termination>True</Termination>
        <Comments>
August 2, 2016: </Comments>
      </Assignment>
    </Assignments>
  </Employee>
</Employees>

This is the code I'm using to pull it, however this gives me a System.NullReferenceException: 这是我用来拉出它的代码,但是这给了我System.NullReferenceException:

private void ImportXMLFile(string p_strFileName) {
    XDocument xEmployees = XDocument.Load(p_strFileName);
    var employees = from employee in xEmployees.Descendants("Employee")
        select new AnEmployee 
    {  //on this line
        FirstName = employee.Element("First_Name").Value,
        LastName = employee.Element("Last_Name").Value,
        EmpID = employee.Element("TSR").Value,
        History = new List<AnAssignment>(from assignment in employee.Descendants("Assignment")
                                         select new AnAssignment
                                         {
                                            Division = assignment.Element("Division").Value,
                                            Project = assignment.Element("Project").Value,
                                            Title = assignment.Element("Title").Value,
                                            StartDate = DateTime.Parse(assignment.Element("Start_Date").Value),
                                            isPartTime = bool.Parse(assignment.Element("Start_Date").Attribute("PartTime").Value),
                                            EndDate = DateTime.Parse(assignment.Element("End_Date").Value),
                                            Supervisor = assignment.Element("Supervisor").Value,
                                            Separation = assignment.Element("SeparationReason").Value,
                                            isTerminated = bool.Parse(assignment.Element("Termination").Value),
                                            Comments = assignment.Element("Comments").Value

                                         })
    };
    foreach(AnEmployee e in employees) {
        EmployeeCollection.add(e);
    }
}

It's doesn't seem to be anything regarding the Employee element, so I'm wondering what I'm doing wrong on the Assignment Element. 似乎与Employee元素无关,所以我想知道我在Assignment元素上做错了什么。 Everything from supervisor to Termination is optional (aka it may or may not appear in a particular assignment. 从主管到终止的所有内容都是可选的(也可以在特定任务中显示也可以不显示)。

Based on the Xml sample Provided the code below : 基于Xml示例提供以下代码:

 Separation = assignment.Element("SeparationReason").Value

should be 应该

 Separation = assignment.Element("Separation_Reason").Value

It's worth nothing here that the code should do null checks (?. if you are using c# 6) to avoid "object reference exceptions" if you anticipate if it doesn't always conform to the schema and values 如果在代码中不进行模式检查(如果使用的是C#6,则应该进行null检查)以避免“对象引用异常”,这在这里一文不值,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM