0
\$\begingroup\$

I am trying to parse xml file using XDocument, basically, I have to select xml element based on requirement and convert into list model.

Below is sample code I am using right now, is there any better way to code or any best practice?

Please suggest.

class Program
{
    static void Main(string[] args)
    {
        XDocument doc = XDocument.Load(@"F:\temp\123.xml");
        var enrollments = PopulateEnrollment(doc);
        var companies = PopulateEmployerTable(doc);
        var plans = PopulatePlan(doc);


    }

    private static List<planTable> PopulatePlan(XDocument doc)
    {
        string planCode = string.Empty;

        var plans = doc.Root
              .Elements("Companies").Elements("Company").Elements("Plans").Elements("Plan")
              .Select(
              x => new planTable
              {
                  TpaId = "MyTPA",
                  PlanName = (string)x.Element("PlanName"),
                  Benefit = (string)x.Element("Benefit"),
                  Code = GetPlanCode(x),
                  BenefitPlanStartDate = GetPlanCode(x).Equals("HSA") ? "20000101" : (string)x.Element("StartDate"),
                  BenefitPlanEndDate = GetPlanCode(x).Equals("HSA") ? "20993101" : (string)x.Element("EndDate"),
                  Type = (string)x.Element("Type"),
                  PlanId = (string)x.Element("CompanyIdentifier"),
                  PartnerPlanID = (string)x.Element("PlanMapping").Element("PartnerPlanId1"),
                  TPAEmployerId = (string)x.Parent.Parent.Element("Identifier"),
                  TPASystemPlanId = (string)x.Element("PlanIdentifier")
              })
              .ToList();

        return plans;
    }


    private static string GetPlanCode(XElement x)
    {
        if ((string)x.Element("PlanMapping").Element("PlanMap") != null)
        {
            return (string)x.Element("PlanMapping").Element("PlanMap").Value;
        }
        return (string)x.Element("Type").Value;
    }



    private static List<EnrollmentTable> PopulateEnrollment(XDocument doc)
    {
        var enrollmentTable = doc.Descendants("Enrollment")
             .Select(
             x => new EnrollmentTable
             {
                 TpaId = "MyCompany",
                 EmployeeId = (string)x.Parent.Element("ExternalEmployeeId"),
                 CoverageLevel = (string)x.Element("CoverageLevel"),
                 PlanStartDate = (string)x.Element("PlanStarts"),
                 PlanEndDate = (string)x.Element("PlanEnds"),
                 CreatedDate = (string)x.Element("Created"),
                 LastModifiedDate = (string)x.Element("LastModified"),
                 PlanCode = (string)x.Element("PlanMapping").Element("PlanMap"),
                 CoverageStartDate = (string)x.Element("CafeteriaData") != null ? (string)x.Element("CafeteriaData").Element("OriginalStartDate") : (string)x.Element("HSAData").Element("EmployeeStartDate"),
                 CoverageEndDate = (string)x.Element("EndDate"),
                 CurrentElection = (string)x.Element("CafeteriaData") != null ? (string)x.Element("CafeteriaData").Element("CurrentElection") : null,
                 PayFrequency = (string)x.Element("CafeteriaData") != null ? (string)x.Element("CafeteriaData").Element("PayPeriods") : null,
                 StartReason = (string)x.Element("StartReason"),
                 PerPayAmount = (string)x.Element("CafeteriaData") != null ? (string)x.Element("CafeteriaData").Element("PerPayAmount") : (string)x.Element("HSAData").Element("PerPayAmount"),
                 EmployerPerPayAmount = (string)x.Element("CafeteriaData") != null ? (string)x.Element("CafeteriaData").Element("EmployerPerPayAmount") : (string)x.Element("HSAData").Element("EmployerPerPayAmount"),
                 AnnualAmount = (string)x.Element("CafeteriaData") != null ? (string)x.Element("CafeteriaData").Element("AnnualAmount") : (string)x.Element("HSAData").Element("YearlyEmployeeAmount"),
                 EmployerAnnualAmount = (string)x.Element("CafeteriaData") != null ? (string)x.Element("CafeteriaData").Element("EmployerAnnualAmount") : (string)x.Element("HSAData").Element("YearlyEmployerAmount")

             })
             .ToList();

        return enrollmentTable;
    }


    private static List<EmployerTable> PopulateEmployerTable(XDocument doc)
    {
        var employers = doc.Root
              .Elements("Companies").Elements("Company")
              .Select(
              x => new EmployerTable
              {
                  TpaId = "MyCompany",
                  SenderId = "EmpNav",
                  Ein = (string)x.Element("TaxID"),
                  Name = (string)x.Element("Name"),
                  State = (string)x.Element("State"),
                  CrossoverDate = (string)System.DateTime.Now.ToShortDateString(),
                  XOFileName = "MyCompany.xml",
                  Status = "MyStatus",
                  LastProcessedDate = (string)System.DateTime.Now.ToShortDateString(),
                  TPAEmployerId = (string)x.Element("Identifier"),
                  IsLatest = true,
                  HasMultiplePayrollGroups = HasMultiplePayrollGroup(x)

              })
              .ToList();

        return employers;
    }

    private static bool HasMultiplePayrollGroup(XElement x)
    {
        bool hasMultiplePayrollGrpup = false;

        if ((string)x.Element("PayrollGroups").Element("PayrollGroup") != null)
        {
            int count = x.Elements("PayrollGroups")
                .Elements("PayrollGroup")
                .Count();
            if (count > 1)
                hasMultiplePayrollGrpup = true;
        }
        return hasMultiplePayrollGrpup;


    }

    class EmployerTable
    {
        public string TpaId { get; set; }
        public string SenderId { get; set; }
        public string Ein { get; set; }
        public string Name { get; set; }
        public string State { get; set; }
        public string CrossoverDate { get; set; }
        public string Status { get; set; }
        public string LastProcessedDate { get; set; }
        public string TPAEmployerId { get; set; }
        public bool HasMultiplePayrollGroups { get; set; }
        public string XOFileName { get; set; }
        public bool IsLatest { get; set; }
    }

    class planTable
    {
        public string TpaId { get; set; }
        public string PlanName { get; set; }
        public string Benefit { get; set; }
        public string Code { get; set; }
        public string BenefitPlanStartDate { get; set; }
        public string BenefitPlanEndDate { get; set; }
        public string Type { get; set; }
        public string PlanId { get; set; }
        public string PartnerPlanID { get; set; }
        public string TPAEmployerId { get; set; }
        public string TPASystemPlanId { get; set; }
        public bool isLatest { get; set; }

    }

    class EnrollmentTable
    {
        public string TpaId { get; set; }
        public string EmployeeId { get; set; }
        public string CoverageLevel { get; set; }
        public string PlanStartDate { get; set; }
        public string PlanEndDate { get; set; }
        public string CreatedDate { get; set; }
        public string LastModifiedDate { get; set; }
        public string PlanCode { get; set; }
        public string ShortCode { get; set; }
        public string TpaEmployerId { get; set; }
        public string CoverageStartDate { get; set; }
        public string PerPayAmount { get; set; }
        public string EmployerPerPayAmount { get; set; }
        public string AnnualAmount { get; set; }
        public string EmployerAnnualAmount { get; set; }
        public string CoverageEndDate { get; set; }
        public string CurrentElection { get; set; }
        public string PayFrequency { get; set; }
        public string StartReason { get; set; }
        public string Status { get; set; }
        public bool isLatest { get; set; }


    }
}
}

Sample XML File

    <?xml version="1.0" encoding="utf-8"?>
    <Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Header>
        <Disclaimer>Data API v0.2</Disclaimer>
        <ExchangeName>MyCompany FLEX</ExchangeName>
        <VendorName>MyCompany</VendorName>
        <VendorCode />
        <TransmissionCode />
        <RunDate>2017-10-04T02:31:25.4327731-04:00</RunDate>
        <EnrollmentType>Current</EnrollmentType>
      </Header>
      <Companies>
        <Company>
          <Identifier>TEST1_FLEX</Identifier>
          <Name>Dunder Mifflin</Name>
          <TaxID>12-123456789</TaxID>
          <Address1>1725 Slough Avenue</Address1>
          <Address2 />
          <City>Scranton</City>
          <State>PA</State>
          <ZIP>77070</ZIP>
          <VoiceNumber />
          <Contacts>
            <Contact>
              <Name>Kathy</Name>
              <Phone>(301) 522-4100</Phone>
              <Email>kcummings@samplecompany.com</Email>
            </Contact>
            <Contact>
              <Name>Meredith Grey</Name>
              <Phone>(301) 854-7716</Phone>
              <Email>Meredith@email.demo</Email>
            </Contact>
            <Contact>
              <Name>Michael McDonald</Name>
              <Phone>(555) 334-0909</Phone>
              <Email>McDonald Insurance Group</Email>
            </Contact>
            <Contact>
              <Name>Ms K</Name>
              <Phone>(281) 377-3939</Phone>
              <Email />
            </Contact>
          </Contacts>
          <Classes>
            <Class>
              <Name>admin</Name>
            </Class>
            <Class>
              <Name>All Full Time Employees</Name>
            </Class>
            <Class>
              <Name>Full Time / Salaried</Name>
            </Class>
            <Class>
              <Name>Officers</Name>
            </Class>
            <Class>
              <Name>Part Time / Hourly</Name>
            </Class>
            <Class>
              <Name>Union</Name>
            </Class>
          </Classes>
          <Departments>
            <Department>
              <Name>Accounting</Name>
            </Department>
            <Department>
              <Name>Customer Service</Name>
            </Department>
            <Department>
              <Name>Operations</Name>
            </Department>
            <Department>
              <Name>Sales</Name>
            </Department>
          </Departments>
          <Divisions>
            <Division>
              <Name>Bel Air</Name>
            </Division>
            <Division>
              <Name>Gaithersburg/Corp</Name>
            </Division>
            <Division>
              <Name>Hagerstown/Field</Name>
            </Division>
          </Divisions>
          <Offices>
            <Office>
              <Name>Bel Air</Name>
              <Address1>123 Test Lane</Address1>
              <Address2 />
              <City>Bel Air</City>
              <State>MD</State>
              <ZIP>21740</ZIP>
            </Office>
            <Office>
              <Name>Gaithersburg</Name>
              <Address1>1 Technology Drive</Address1>
              <Address2>Suite 500</Address2>
              <City>Rockville</City>
              <State>MD</State>
              <ZIP>20877</ZIP>
            </Office>
            <Office>
              <Name>Hagerstown</Name>
              <Address1>20 Data Drive</Address1>
              <Address2 />
              <City>Germantown</City>
              <State>MD</State>
              <ZIP>20880</ZIP>
            </Office>
          </Offices>
          <BusinessUnits>
            <BusinessUnit>
              <Name>Accounting</Name>
              <TaxID>12-5555555</TaxID>
            </BusinessUnit>
            <BusinessUnit>
              <Name>Customer Service</Name>
              <TaxID>12-0000912</TaxID>
            </BusinessUnit>
            <BusinessUnit>
              <Name>Operations</Name>
              <TaxID>11-0000021</TaxID>
            </BusinessUnit>
            <BusinessUnit>
              <Name>Sales</Name>
              <TaxID>14-0000031</TaxID>
            </BusinessUnit>
          </BusinessUnits>
          <PayrollGroups>
            <PayrollGroup>
              <Name>Executive payroll</Name>
              <PayFrequency>24</PayFrequency>
            </PayrollGroup>
            <PayrollGroup>
              <Name>Medical Spending Account BW26</Name>
              <PayFrequency>26</PayFrequency>
            </PayrollGroup>
            <PayrollGroup>
              <Name>Sample Payroll BW24</Name>
              <PayFrequency>24</PayFrequency>
            </PayrollGroup>
            <PayrollGroup>
              <Name>test</Name>
              <PayFrequency>24</PayFrequency>
            </PayrollGroup>
          </PayrollGroups>
          <Plans>
            <Plan>
              <CompanyIdentifier>TEST1_FLEX</CompanyIdentifier>
              <Carrier />
              <PlanName>Medical Flexible Spending Account 2014</PlanName>
              <Benefit>Flexible Spending Account</Benefit>
              <Type>Flexible Spending Account</Type>
              <StartDate>2014-01-01T00:00:00</StartDate>
              <EndDate>2014-12-31T00:00:00</EndDate>
              <CarrierPlanCode />
              <CarrierPlanTypeCode />
              <PolicyNumber>123456</PolicyNumber>
              <IsIssueAgePlan>false</IsIssueAgePlan>
              <IsPostTax>false</IsPostTax>
              <PlanMapping />
              <IsSelfBill xsi:nil="true" />
              <IsSelfFunded>false</IsSelfFunded>
              <PlanIdentifier>2881cce8-c4d5-4db6-9723-f45f3fba991f</PlanIdentifier>
            </Plan>
            <Plan>
              <CompanyIdentifier>TEST1_FLEX</CompanyIdentifier>
              <Carrier>HSA Bank</Carrier>
              <PlanName>HSA 2015</PlanName>
              <Benefit>Consumer Directed Health</Benefit>
              <Type>Healthcare Savings Account</Type>
              <StartDate>2015-01-01T00:00:00</StartDate>
              <EndDate>2015-12-31T00:00:00</EndDate>
              <CarrierPlanCode />
              <CarrierPlanTypeCode />
              <PolicyNumber />
              <IsIssueAgePlan>false</IsIssueAgePlan>
              <IsPostTax>false</IsPostTax>
              <PlanMapping />
              <IsSelfBill xsi:nil="true" />
              <IsSelfFunded>false</IsSelfFunded>
              <PlanIdentifier>05aed29a-d788-4872-b500-8a84fe3d9b1f</PlanIdentifier>
            </Plan>
            <Plan>
              <CompanyIdentifier>TEST1_FLEX</CompanyIdentifier>
              <Carrier />
              <PlanName>Dependent Spending Account 2016</PlanName>
              <Benefit>Dependent Care Spending Account</Benefit>
              <Type>Dependent Care Spending Account</Type>
              <StartDate>2016-01-01T00:00:00</StartDate>
              <EndDate>2016-12-31T00:00:00</EndDate>
              <CarrierPlanCode />
              <CarrierPlanTypeCode />
              <PolicyNumber />
              <IsIssueAgePlan>false</IsIssueAgePlan>
              <IsPostTax>false</IsPostTax>
              <PlanMapping />
              <IsSelfBill xsi:nil="true" />
              <IsSelfFunded>false</IsSelfFunded>
              <PlanIdentifier>c84a0658-f071-421a-b99f-e98c5e0e3f32</PlanIdentifier>
            </Plan>
            <Plan>
              <CompanyIdentifier>TEST1_FLEX</CompanyIdentifier>
              <Carrier />
              <PlanName>Flexible Spending Account 2016</PlanName>
              <Benefit>Flexible Spending Account</Benefit>
              <Type>Flexible Spending Account</Type>
              <StartDate>2016-01-01T00:00:00</StartDate>
              <EndDate>2016-12-31T00:00:00</EndDate>
              <CarrierPlanCode />
              <CarrierPlanTypeCode />
              <PolicyNumber>123456</PolicyNumber>
              <IsIssueAgePlan>false</IsIssueAgePlan>
              <IsPostTax>false</IsPostTax>
              <PlanMapping />
              <IsSelfBill xsi:nil="true" />
              <IsSelfFunded>false</IsSelfFunded>
              <PlanIdentifier>3be6447a-dbcf-4a52-bf73-880c0a85e8dc</PlanIdentifier>
            </Plan>
            <Plan>
              <CompanyIdentifier>TEST1_FLEX</CompanyIdentifier>
              <Carrier>Unspecified Carrier</Carrier>
              <PlanName>HRA</PlanName>
              <Benefit>Consumer Directed Health</Benefit>
              <Type>Healthcare Reimbursement Account</Type>
              <StartDate>2016-01-01T00:00:00</StartDate>
              <EndDate>2016-12-31T00:00:00</EndDate>
              <CarrierPlanCode />
              <CarrierPlanTypeCode />
              <PolicyNumber />
              <IsIssueAgePlan>false</IsIssueAgePlan>
              <IsPostTax>false</IsPostTax>
              <PlanMapping />
              <IsSelfBill xsi:nil="true" />
              <IsSelfFunded>false</IsSelfFunded>
              <PlanIdentifier>a83b7e3e-88e2-417c-911f-6418d03d72f3</PlanIdentifier>
            </Plan>
            <Plan>
              <CompanyIdentifier>TEST1_FLEX</CompanyIdentifier>
              <Carrier>HSA Bank</Carrier>
              <PlanName>HSA 2016</PlanName>
              <Benefit>Consumer Directed Health</Benefit>
              <Type>Healthcare Savings Account</Type>
              <StartDate>2016-01-01T00:00:00</StartDate>
              <EndDate>2016-12-31T00:00:00</EndDate>
              <CarrierPlanCode />
              <CarrierPlanTypeCode />
              <PolicyNumber />
              <IsIssueAgePlan>false</IsIssueAgePlan>
              <IsPostTax>false</IsPostTax>
              <PlanMapping />
              <IsSelfBill xsi:nil="true" />
              <IsSelfFunded>false</IsSelfFunded>
              <PlanIdentifier>d0d5fafe-98ae-4a8a-ae50-179cbb457860</PlanIdentifier>
            </Plan>
            <Plan>
              <CompanyIdentifier>TEST1_FLEX</CompanyIdentifier>
              <Carrier />
              <PlanName>Parking Reimbursement (2016)</PlanName>
              <Benefit>Parking Reimbursement</Benefit>
              <Type>Parking Reimbursement</Type>
              <StartDate>2016-01-01T00:00:00</StartDate>
              <EndDate>2016-12-31T00:00:00</EndDate>
              <CarrierPlanCode />
              <CarrierPlanTypeCode />
              <PolicyNumber />
              <IsIssueAgePlan>false</IsIssueAgePlan>
              <IsPostTax>false</IsPostTax>
              <PlanMapping />
              <IsSelfBill xsi:nil="true" />
              <IsSelfFunded>false</IsSelfFunded>
              <PlanIdentifier>ab8ec95d-5ded-40c7-b101-02f84cbf3db6</PlanIdentifier>
            </Plan>
            <Plan>
              <CompanyIdentifier>TEST1_FLEX</CompanyIdentifier>
              <Carrier />
              <PlanName>Transit Reimbursement 2016</PlanName>
              <Benefit>Transit Reimbursement</Benefit>
              <Type>Transit Reimbursement</Type>
              <StartDate>2016-01-01T00:00:00</StartDate>
              <EndDate>2016-12-31T00:00:00</EndDate>
              <CarrierPlanCode />
              <CarrierPlanTypeCode />
              <PolicyNumber />
              <IsIssueAgePlan>false</IsIssueAgePlan>
              <IsPostTax>false</IsPostTax>
              <PlanMapping />
              <IsSelfBill xsi:nil="true" />
              <IsSelfFunded>false</IsSelfFunded>
              <PlanIdentifier>10406ead-0472-454a-b7e7-ab140859cb2c</PlanIdentifier>
            </Plan>
            <Plan>
              <CompanyIdentifier>TEST1_FLEX</CompanyIdentifier>
              <Carrier>Unspecified Carrier</Carrier>
              <PlanName>2017 HRA</PlanName>
              <Benefit>Consumer Directed Health</Benefit>
              <Type>Healthcare Reimbursement Account</Type>
              <StartDate>2017-01-01T00:00:00</StartDate>
              <EndDate>2017-12-31T00:00:00</EndDate>
              <CarrierPlanCode />
              <CarrierPlanTypeCode />
              <PolicyNumber>3345</PolicyNumber>
              <IsIssueAgePlan>false</IsIssueAgePlan>
              <IsPostTax>false</IsPostTax>
              <PlanMapping />
              <IsSelfBill xsi:nil="true" />
              <IsSelfFunded>false</IsSelfFunded>
              <PlanIdentifier>6ffa6dcf-59e2-4d0e-a5ec-c28ab864a3a8</PlanIdentifier>
            </Plan>
            <Plan>
              <CompanyIdentifier>TEST1_FLEX</CompanyIdentifier>
              <Carrier />
              <PlanName>2017 LPFSA</PlanName>
              <Benefit>Limited Purpose FSA</Benefit>
              <Type>Limited Purpose FSA</Type>
              <StartDate>2017-01-01T00:00:00</StartDate>
              <EndDate>2017-12-31T00:00:00</EndDate>
              <CarrierPlanCode />
              <CarrierPlanTypeCode />
              <PolicyNumber>1123</PolicyNumber>
              <IsIssueAgePlan>false</IsIssueAgePlan>
              <IsPostTax>false</IsPostTax>
              <PlanMapping />
              <IsSelfBill xsi:nil="true" />
              <IsSelfFunded>false</IsSelfFunded>
              <PlanIdentifier>2d7dba8f-aedc-477e-a49d-d2801b3e79c5</PlanIdentifier>
            </Plan>
            <Plan>
              <CompanyIdentifier>TEST1_FLEX</CompanyIdentifier>
              <Carrier />
              <PlanName>Flexible Spending Account 2017</PlanName>
              <Benefit>Flexible Spending Account</Benefit>
              <Type>Flexible Spending Account</Type>
              <StartDate>2017-01-01T00:00:00</StartDate>
              <EndDate>2017-12-31T00:00:00</EndDate>
              <CarrierPlanCode />
              <CarrierPlanTypeCode />
              <PolicyNumber>123456</PolicyNumber>
              <IsIssueAgePlan>false</IsIssueAgePlan>
              <IsPostTax>false</IsPostTax>
              <PlanMapping />
              <IsSelfBill xsi:nil="true" />
              <IsSelfFunded>false</IsSelfFunded>
              <PlanIdentifier>5259cd33-872a-46ee-8870-143fc79018b1</PlanIdentifier>
            </Plan>
            <Plan>
              <CompanyIdentifier>TEST1_FLEX</CompanyIdentifier>
              <Carrier>HSA Bank</Carrier>
              <PlanName>HSA 2017</PlanName>
              <Benefit>Consumer Directed Health</Benefit>
              <Type>Healthcare Savings Account</Type>
              <StartDate>2017-01-01T00:00:00</StartDate>
              <EndDate>2017-12-31T00:00:00</EndDate>
              <CarrierPlanCode />
              <CarrierPlanTypeCode />
              <PolicyNumber />
              <IsIssueAgePlan>false</IsIssueAgePlan>
              <IsPostTax>false</IsPostTax>
              <PlanMapping />
              <IsSelfBill xsi:nil="true" />
              <IsSelfFunded>false</IsSelfFunded>
              <PlanIdentifier>6d6176c7-2d1b-489d-a173-2bca69c96d3e</PlanIdentifier>
            </Plan>
          </Plans>
          <Employees>
            <Employee>
              <ExternalEmployeeId>19151</ExternalEmployeeId>
              <CompanyIdentifier>TEST1_FLEX</CompanyIdentifier>
              <SSN>123456789</SSN>
              <FirstName>Wilson</FirstName>
              <MiddleName />
              <LastName>Abner</LastName>
              <Gender>M</Gender>
              <DOB>1965-06-15T00:00:00</DOB>
              <MaritalStatus>Married</MaritalStatus>
              <Address1>21 Wilson Lane</Address1>
              <Address2 />
              <City>gaithersburg</City>
              <State>MD</State>
              <County />
              <ZIP>20850</ZIP>
              <Country />
              <Email>wabner@123.com</Email>
              <Phone>(301) 123-9922</Phone>
              <PhoneExtension xsi:nil="true" />
              <HireDate>2011-11-15T00:00:00</HireDate>
              <HiredOn>2010-03-02T17:18:00</HiredOn>
              <Salary>137000.00</Salary>
              <SalaryEffectiveDate>2013-04-23T00:00:00</SalaryEffectiveDate>
              <LastSalaryReviewDate xsi:nil="true" />
              <PayFrequency>24</PayFrequency>
              <WeeklyHours>40</WeeklyHours>
              <EmployeeNumber />
              <PayrollID />
              <EmploymentStatus>Active</EmploymentStatus>
              <LeaveStart xsi:nil="true" />
              <LeaveEnd xsi:nil="true" />
              <JobTitle>Director of Operations</JobTitle>
              <RetiredDate xsi:nil="true" />
              <TerminationDate xsi:nil="true" />
              <TerminatedOn xsi:nil="true" />
              <Class>Full Time / Salaried</Class>
              <Department>Operations</Department>
              <Division>Bel Air</Division>
              <Office>Bel Air</Office>
              <PayrollGroup>Sample Payroll BW24</PayrollGroup>
              <Disabled xsi:nil="true" />
              <TobaccoUser>true</TobaccoUser>
              <TobaccoSignatureDate>2011-12-16T00:00:00</TobaccoSignatureDate>
              <USCitizen xsi:nil="true" />
              <DisabledStartDate xsi:nil="true" />
              <DisabledEndDate xsi:nil="true" />
              <ClassEffectiveDate xsi:nil="true" />
              <Created>2010-01-15T15:50:55.283</Created>
              <DemographicLastModified>2017-01-06T10:36:57.78</DemographicLastModified>
              <HrisComplete>true</HrisComplete>
              <Dependents>
                <Dependent>
                  <SSN>100200300</SSN>
                  <SequenceNumber>1</SequenceNumber>
                  <FirstName>Tori</FirstName>
                  <MiddleName />
                  <LastName>Abner</LastName>
                  <Relationship>Spouse</Relationship>
                  <Gender>F</Gender>
                  <DOB>1972-02-21T00:00:00</DOB>
                  <AddressSameAsEmployee>true</AddressSameAsEmployee>
                  <Address1>21 Wilson Lane</Address1>
                  <Address2 />
                  <City>gaithersburg</City>
                  <State>MD</State>
                  <County />
                  <ZIP>20850</ZIP>
                  <Country />
                  <Phone />
                  <Student>false</Student>
                  <Disabled>false</Disabled>
                  <TobaccoUser>false</TobaccoUser>
                  <TobaccoSignatureDate xsi:nil="true" />
                  <DisabledStartDate xsi:nil="true" />
                  <DisabledEndDate xsi:nil="true" />
                  <StudentStartDate xsi:nil="true" />
                  <StudentEndDate xsi:nil="true" />
                  <Created>2010-01-18T15:16:16.06</Created>
                  <LastModified>2013-04-24T14:57:09.813</LastModified>
                </Dependent>
                <Dependent>
                  <SSN>111222333</SSN>
                  <SequenceNumber>2</SequenceNumber>
                  <FirstName>Todd</FirstName>
                  <MiddleName />
                  <LastName>Abner</LastName>
                  <Relationship>Child</Relationship>
                  <Gender>M</Gender>
                  <DOB>2009-07-06T00:00:00</DOB>
                  <AddressSameAsEmployee>true</AddressSameAsEmployee>
                  <Address1>21 Wilson Lane</Address1>
                  <Address2 />
                  <City>gaithersburg</City>
                  <State>MD</State>
                  <County />
                  <ZIP>20850</ZIP>
                  <Country />
                  <Student>false</Student>
                  <Disabled>false</Disabled>
                  <TobaccoUser xsi:nil="true" />
                  <TobaccoSignatureDate xsi:nil="true" />
                  <DisabledStartDate xsi:nil="true" />
                  <DisabledEndDate xsi:nil="true" />
                  <StudentStartDate xsi:nil="true" />
                  <StudentEndDate xsi:nil="true" />
                  <Created>2010-01-18T15:17:01.81</Created>
                  <LastModified>2010-01-18T15:17:01.81</LastModified>
                </Dependent>
              </Dependents>
              <Enrollments>
                <Enrollment>
                  <EnrollmentType>Current</EnrollmentType>
                  <Plan>Medical Flexible Spending Account 2014</Plan>
                  <GroupNumber>123456</GroupNumber>
                  <PolicyNumber>123456</PolicyNumber>
                  <Benefit>Flexible Spending Account</Benefit>
                  <Type>Flexible Spending Account</Type>
                  <PlanStarts>2014-01-01T00:00:00</PlanStarts>
                  <PlanEnds>2014-12-31T00:00:00</PlanEnds>
                  <CoverageLevel />
                  <StartDate>2014-07-04T00:00:00</StartDate>
                  <EnrolledOn>2014-07-03T14:46:05</EnrolledOn>
                  <EndDate xsi:nil="true" />
                  <EndedOn xsi:nil="true" />
                  <SignDate>2014-07-03T00:00:00</SignDate>
                  <EmployeeCost xsi:nil="true" />
                  <EmployerCost xsi:nil="true" />
                  <DefinedContributionAmount xsi:nil="true" />
                  <PlanCost xsi:nil="true" />
                  <BenefitAmount xsi:nil="true" />
                  <StartReason>Loss of dependent child status (36 months)</StartReason>
                  <PCPEffectiveDate xsi:nil="true" />
                  <AttainedAge>false</AttainedAge>
                  <CarrierPlanCode />
                  <EmployeeCovered>true</EmployeeCovered>
                  <SpouseCovered>false</SpouseCovered>
                  <DomesticPartnerCovered>false</DomesticPartnerCovered>
                  <ChildrenCovered>0</ChildrenCovered>
                  <CafeteriaData>
                    <CurrentElection>3000.00</CurrentElection>
                    <PerPayAmount>250.00</PerPayAmount>
                    <PayPeriods>12</PayPeriods>
                    <EmployerPerPayAmount>0.00</EmployerPerPayAmount>
                    <EmployerAnnualAmount>0.00</EmployerAnnualAmount>
                    <OriginalStartDate>2014-07-04T00:00:00</OriginalStartDate>
                    <AnnualAmount>3000.00</AnnualAmount>
                  </CafeteriaData>
                  <Created>2014-07-03T14:45:59.65</Created>
                  <LastModified>2014-07-03T14:45:59.65</LastModified>
                  <DependentEnrollees />
                  <Beneficiaries />
                  <PlanMapping />
                  <EnrollmentMapping />
                  <PlanIdentifier>2881cce8-c4d5-4db6-9723-f45f3fba991f</PlanIdentifier>
                </Enrollment>
              </Enrollments>
              <CobraEnrollments />
              <FutureSalaries />
              <MappedClassification>TEST1_FLEX</MappedClassification>
              <AnnualBenefitSalary xsi:nil="true" />
              <EmployeeMapping />
              <AnnualBenefitSalaryEffectiveDate xsi:nil="true" />
            </Employee>
            <Employee>
              <ExternalEmployeeId>226483</ExternalEmployeeId>
              <CompanyIdentifier>TEST1_FLEX</CompanyIdentifier>
              <SSN>556655668</SSN>
              <FirstName>Kyle</FirstName>
              <MiddleName />
              <LastName>Adams</LastName>
              <Gender>M</Gender>
              <DOB>1969-01-01T00:00:00</DOB>
              <MaritalStatus>Married</MaritalStatus>
              <Address1>1 street</Address1>
              <Address2 />
              <City>Gaithersburg</City>
              <State>MD</State>
              <County />
              <ZIP>20878</ZIP>
              <Country>US</Country>
              <Email>kadams@dundermifflin.com</Email>
              <Phone>(123) 090-8909</Phone>
              <PhoneExtension xsi:nil="true" />
              <HireDate>2013-12-01T00:00:00</HireDate>
              <HiredOn>2014-01-07T00:00:00</HiredOn>
              <Salary>100000.00</Salary>
              <SalaryEffectiveDate>2014-12-01T00:00:00</SalaryEffectiveDate>
              <LastSalaryReviewDate xsi:nil="true" />
              <PayFrequency>24</PayFrequency>
              <WeeklyHours>40</WeeklyHours>
              <EmployeeNumber />
              <PayrollID />
              <EmploymentStatus>Active</EmploymentStatus>
              <LeaveStart xsi:nil="true" />
              <LeaveEnd xsi:nil="true" />
              <JobTitle>Director of Business Development</JobTitle>
              <RetiredDate xsi:nil="true" />
              <TerminationDate xsi:nil="true" />
              <TerminatedOn xsi:nil="true" />
              <Class>Full Time / Salaried</Class>
              <Division>Gaithersburg/Corp</Division>
              <Office>Gaithersburg</Office>
              <PayrollGroup>Sample Payroll BW24</PayrollGroup>
              <Disabled xsi:nil="true" />
              <TobaccoUser xsi:nil="true" />
              <TobaccoSignatureDate xsi:nil="true" />
              <USCitizen xsi:nil="true" />
              <DisabledStartDate xsi:nil="true" />
              <DisabledEndDate xsi:nil="true" />
              <ClassEffectiveDate xsi:nil="true" />
              <Created>2014-01-07T14:47:59.31</Created>
              <DemographicLastModified>2017-01-06T09:57:27.687</DemographicLastModified>
              <HrisComplete>true</HrisComplete>
              <Dependents>
                <Dependent>
                  <SSN />
                  <SequenceNumber>1</SequenceNumber>
                  <FirstName>Sarah</FirstName>
                  <MiddleName />
                  <LastName>Adams</LastName>
                  <Relationship>Spouse</Relationship>
                  <Gender>F</Gender>
                  <DOB>1970-01-01T00:00:00</DOB>
                  <AddressSameAsEmployee>true</AddressSameAsEmployee>
                  <Address1>1 street</Address1>
                  <Address2 />
                  <City>Gaithersburg</City>
                  <State>MD</State>
                  <County />
                  <ZIP>20878</ZIP>
                  <Country />
                  <Phone />
                  <Student>false</Student>
                  <Disabled>false</Disabled>
                  <TobaccoUser xsi:nil="true" />
                  <TobaccoSignatureDate xsi:nil="true" />
                  <DisabledStartDate xsi:nil="true" />
                  <DisabledEndDate xsi:nil="true" />
                  <StudentStartDate xsi:nil="true" />
                  <StudentEndDate xsi:nil="true" />
                  <Created>2014-01-07T14:48:48.147</Created>
                  <LastModified>2014-01-07T14:48:48.147</LastModified>
                </Dependent>
                <Dependent>
                  <SSN />
                  <SequenceNumber>2</SequenceNumber>
                  <FirstName>Daisy</FirstName>
                  <MiddleName />
                  <LastName>Adams</LastName>
                  <Relationship>Child</Relationship>
                  <Gender>F</Gender>
                  <DOB>2010-01-01T00:00:00</DOB>
                  <AddressSameAsEmployee>true</AddressSameAsEmployee>
                  <Address1>1 street</Address1>
                  <Address2 />
                  <City>Gaithersburg</City>
                  <State>MD</State>
                  <County />
                  <ZIP>20878</ZIP>
                  <Country />
                  <Phone />
                  <Student>false</Student>
                  <Disabled>false</Disabled>
                  <TobaccoUser xsi:nil="true" />
                  <TobaccoSignatureDate xsi:nil="true" />
                  <DisabledStartDate xsi:nil="true" />
                  <DisabledEndDate xsi:nil="true" />
                  <StudentStartDate xsi:nil="true" />
                  <StudentEndDate xsi:nil="true" />
                  <Created>2014-01-07T14:49:03.287</Created>
                  <LastModified>2014-01-07T14:49:03.287</LastModified>
                </Dependent>
              </Dependents>
              <Enrollments>
                <Enrollment>
                  <EnrollmentType>Current</EnrollmentType>
                  <Plan>Medical Flexible Spending Account 2014</Plan>
                  <GroupNumber>123456</GroupNumber>
                  <PolicyNumber>123456</PolicyNumber>
                  <Benefit>Flexible Spending Account</Benefit>
                  <Type>Flexible Spending Account</Type>
                  <PlanStarts>2014-01-01T00:00:00</PlanStarts>
                  <PlanEnds>2014-12-31T00:00:00</PlanEnds>
                  <CoverageLevel />
                  <StartDate>2014-01-01T00:00:00</StartDate>
                  <EnrolledOn>2014-01-07T15:50:05</EnrolledOn>
                  <EndDate xsi:nil="true" />
                  <EndedOn xsi:nil="true" />
                  <SignDate>2014-01-07T00:00:00</SignDate>
                  <EmployeeCost xsi:nil="true" />
                  <EmployerCost xsi:nil="true" />
                  <DefinedContributionAmount xsi:nil="true" />
                  <PlanCost xsi:nil="true" />
                  <BenefitAmount xsi:nil="true" />
                  <StartReason>New Hire</StartReason>
                  <PCPEffectiveDate xsi:nil="true" />
                  <AttainedAge>false</AttainedAge>
                  <CarrierPlanCode />
                  <EmployeeCovered>true</EmployeeCovered>
                  <SpouseCovered>false</SpouseCovered>
                  <DomesticPartnerCovered>false</DomesticPartnerCovered>
                  <ChildrenCovered>0</ChildrenCovered>
                  <CafeteriaData>
                    <CurrentElection>312.00</CurrentElection>
                    <PerPayAmount>12.00</PerPayAmount>
                    <PayPeriods>26</PayPeriods>
                    <EmployerPerPayAmount>0.00</EmployerPerPayAmount>
                    <EmployerAnnualAmount>0.00</EmployerAnnualAmount>
                    <OriginalStartDate>2014-01-01T00:00:00</OriginalStartDate>
                    <AnnualAmount>312.00</AnnualAmount>
                  </CafeteriaData>
                  <Created>2014-01-07T15:50:01.227</Created>
                  <LastModified>2014-01-07T15:50:01.227</LastModified>
                  <DependentEnrollees />
                  <Beneficiaries />
                  <PlanMapping />
                  <EnrollmentMapping />
                  <PlanIdentifier>2881cce8-c4d5-4db6-9723-f45f3fba991f</PlanIdentifier>
                </Enrollment>
              </Enrollments>
              <CobraEnrollments />
              <FutureSalaries />
              <MappedClassification>TEST1_FLEX</MappedClassification>
              <AnnualBenefitSalary xsi:nil="true" />
              <EmployeeMapping />
              <AnnualBenefitSalaryEffectiveDate xsi:nil="true" />
            </Employee>
          </Employees>
          <CorporationType>LLC</CorporationType>
        </Company>

      </Companies>
    </Data>
\$\endgroup\$
  • \$\begingroup\$ Could you add a sample xml? \$\endgroup\$ – t3chb0t Nov 11 '17 at 9:17
  • \$\begingroup\$ And please update the title so that is says what your code is doing not your question about it. \$\endgroup\$ – t3chb0t Nov 11 '17 at 9:18
  • \$\begingroup\$ @t3chb0t I have added sample XML \$\endgroup\$ – M005 Nov 11 '17 at 9:25
  • \$\begingroup\$ Have you considered actually parsing the document using XML serializers or Data Contracts? I always cringe when I see these kinds of XDocument constructs outside of very, very simple or very generic XML usage. \$\endgroup\$ – jessehouwing Nov 11 '17 at 10:01
  • \$\begingroup\$ @jessehouwing: Devil's advocate: this example may be the easiest example of a bunch of XML conversions. If other more complex conversions warrant a manual XDocument construct, then it's acceptable to use the same method for a simple conversion, in the interest of maintaining code consistency. \$\endgroup\$ – Flater Nov 13 '17 at 16:25
1
\$\begingroup\$

Since you are not reading your Xml 1:1 into a data object but you use a lot of conditions there to get alternative elements like here:

EmployerPerPayAmount = (string)x.Element("CafeteriaData") != null ? (string)x.Element("CafeteriaData").Element("EmployerPerPayAmount") : (string)x.Element("HSAData").Element("EmployerPerPayAmount"),

I suggest reading about XSLT - Transformation first. With this you can write a script that will transform your input Xml to another Xml that exactly matches your classes.

\$\endgroup\$
  • \$\begingroup\$ Let me try this \$\endgroup\$ – M005 Nov 13 '17 at 4:34
0
\$\begingroup\$

Best way is to use the XML Schema definition tool to turn your xsd definition into c# classes. Then, all you need is to use .Net native xml serializer to serialize and deserializer your data.

Instead of adding code. I'll follow you to this question which has a great explanation on how to modeling xml serializable classes and how to use the xml serializer

\$\endgroup\$
  • \$\begingroup\$ Quote from wiki: It can be used by programmers to verify each piece of item content in a document. They can check if it adheres to the description of the element it is placed in. - This won't help you to import the file into a class if that class has a different structure like it does here. You'll still need to transform it to something else. \$\endgroup\$ – t3chb0t Nov 12 '17 at 8:13
  • \$\begingroup\$ @t3chb0t: Agree, but conversion between two classes (strongly typed) is notably easier than conversion between an XML structure and a class with a different structure (stringly typed). This answer may not be complete, but its suggestion still dramatically improves the existing code quality. \$\endgroup\$ – Flater Nov 13 '17 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.