简体   繁体   中英

Parsing complex XML with LINQ

I have the following XML that I have generated:

<?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:

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. 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 :

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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