开发者

c#: Using Linq to get a IEnumerable collection of XML Nodes to Traverse

I have some code that is using the following xml file. Can someone tell me why my doc.Descendants is returning null?

Here is the XML File:

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
 <EmployerTPASeparationResponseCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://uidataexchange.org/schemas SeparationResponse.xsd" xmlns="https://uidataexchange.org/schemas">
   <EmployerTPASeparationResponse>
    <StateRequestRecordGUID>94321098761987654321323456109883</StateRequestRecordGUID>
    <BrokerRecordTransactionNumber>123456789</BrokerRecordTransactionNumber>
    <SSN>999999999</SSN>
    <ClaimEffectiveDate>2011-02-09</ClaimEffectiveDate>
    <ClaimNumber>1234568010</ClaimNumber>
    <StateEmployerAccountNbr>12345689</StateEmployerAccountNbr>
    <ClaimantJobTitle>Assistant Manager</ClaimantJobTitle>
    <EmployerReportedClaimantFirstDayofWork>2009-02-02</EmployerReportedClaimantFirstDayofWork>
    <EmployerReportedClaimantLastDayofWork>2010-03-16</EmployerReportedClaimantLastDayofWork>
    <TotalEarnedWagesNeededInd>2</TotalEarnedWagesNeededInd>
    <TotalEarnedWages>15000.00</TotalEarnedWages>
    <WagesEarnedAfterClaimEffectiveDate>20000.00</WagesEarnedAfterClaimEffectiveDate>
<NumberOfHoursWorkedAfterClaimEffectiveDate>80</NumberOfHoursWorkedAfterClaimEffectiveDate>
    <AverageWeeklyWage>10.00</AverageWeeklyWage>
    <EmployerSepReasonCode>2</EmployerSepReasonCode>
    <EmployerSepReasonComments>Expected return date back to work is 4/25/11</EmployerSepReasonComments>
    <PreparerTypeCode>T</PreparerTypeCode>
    <PreparerCompanyName>Barnett Associates</PreparerCompanyName> 
    <PreparerTelephoneNumberPlusExt>5555555555</PreparerTelephoneNumberPlusExt>
    <PreparerContactName>Diana Turkoane</PreparerContactName>
    <PreparerTitle>CSR</PreparerTitle>
    <PreparerFaxNbr>5555555555</PreparerFaxNbr>
    <PreparerEmailAddress>asdf@asdf.com</PreparerEmailAddress>
  </EmployerTPASeparationResponse>
    <EmployerTPASeparationResponse>
    <StateRequestRecordGUID>94321098761987654321323456109884</StateRequestRecordGUID>
    <BrokerRecordTransactionNumber>123456789</BrokerRecordTransactionNumber>
    <SSN>999999999</SSN>
    <ClaimEffectiveDate>2011-02-10</ClaimEffectiveDate>
    <ClaimNumber>1234568010</ClaimNumber>
    <StateEmployerAccountNbr>12345689</StateEmployerAccountNbr>
    <ClaimantJobTitle>Assistant Manager</ClaimantJobTitle>
    <EmployerReportedClaimantFirstDayofWork>2009-02-03</EmployerReportedClaimantFirstDayofWork>
    <EmployerReportedClaimantLastDayofWork>2010-03-17</EmployerReportedClaimantLastDayofWork>
    <EmployerSepReasonCode>2</EmployerSepReasonCode>
    <EmployerSepReasonComments>Expected return date back to work is 4/30/11</EmployerSepReasonComments>
    <PreparerTypeCode>T</PreparerTypeCode>
    <PreparerCompanyName>Barnett Associates</PreparerCompanyName>
    <PreparerTelephoneNumberPlusExt>5555555555</PreparerTelephoneNumberPlusExt>
    <PreparerContactName>Diana Turkoane</PreparerContactName>
    <PreparerTitle>CSR</PreparerT开发者_运维百科itle>
    <PreparerFaxNbr>5555555555</PreparerFaxNbr>
    <PreparerEmailAddress>asdf@asdf.com</PreparerEmailAddress>
  </EmployerTPASeparationResponse>
</EmployerTPASeparationResponseCollection>

Here is the code I am using. I first use some Linq to get the Anonymous values, then try to loop through each child node and populate with cmd.parameters.

Here is the code:

XDocument doc = XDocument.Load("XmlString.xml");

var EmployerTPASeparationResponse = 
    from node in doc.Descendants("EmployerTPASeparationResponse")
    select new
    {
        param1 = node.Element("StateRequestRecordGUID").Value,
        param2 = node.Element("BrokerRecordTransactionNumber").Value,
        param3 = node.Element("SSN").Value,
        param4 = node.Element("ClaimEffectiveDate").Value,
        param5 = node.Element("ClaimNumber").Value,
        param6 = node.Element("StateEmployerAccountNbr").Value,
        param7 = node.Element("CorrectedEmployerName").Value,
        param8 = node.Element("CorrectedStateEmployerAccountNbr").Value,
        param9 = node.Element("CorrectedFEIN").Value,
        param10 = node.Element("OtherSSN").Value,
        param11 = node.Element("ClaimantNameWorkedAsForEmployers").Value,
        param12 = node.Element("ClaimantJobTitle").Value,
        param13 = node.Element("SeasonalEmploymentInd").Value,
        param14 = node.Element("EmployerReportedClaimantFirstDayofWork").Value,
        param15 = node.Element("EmployerReportedClaimantLastDayofWork").Value,
        param16 = node.Element("EffectiveSeparationDate").Value,
        param17 = node.Element("TotalEarnedWagesNeededInd").Value,
        param18 = node.Element("TotalEarnedWages").Value,
        param19 = node.Element("TotalWeeksWorkedNeededInd").Value,
        param20 = node.Element("TotalWeeksWorked").Value,
        param21 = node.Element("WagesEarnedAfterClaimEffectiveDate").Value,
        param22 = node.Element("NumberOfHoursWorkedAfterClaimEffectiveDate").Value,
        param23 = node.Element("AverageWeeklyWage").Value,
        param24 = node.Element("EmployerSepReasonCode").Value,
        param25 = node.Element("ReturnToWorkInd").Value,
        param26 = node.Element("ReturnToWorkDate").Value,
        param27 = node.Element("WorkingAllAvailableHoursInd").Value,
        param28 = node.Element("NotWorkingAvailableHoursReason").Value,
        param29 = node.Element("DischargeReasonCode").Value,
        param30 = node.Element("FinalIncidentReason").Value,
        param31 = node.Element("FinalIncidentDate").Value,
        param32 = node.Element("ViolateCompanyPolicyInd").Value,
        param33 = node.Element("DischargePolicyAwareInd").Value,
        param34 = node.Element("DischargePolicyAwareExplanationCode").Value,
        param35 = node.Element("WhoDischargedName").Value,
        param36 = node.Element("WhoDischargedTitle").Value,
        param37 = node.Element("DischargeReasonComments").Value,
        param38 = node.Element("VoluntarySepReasonCode").Value,
        param39 = node.Element("HiringAgreementChangesCode").Value,
        param40 = node.Element("HiringAgreementChangeComments").Value,
        param41 = node.Element("ClaimantActionstoAvoidQuitInd").Value,
        param42 = node.Element("ActionTakenComments").Value,
        param43 = node.Element("ContinuingWorkAvailableInd").Value,
        param44 = node.Element("VoluntarySepReasonComments").Value,
        param45 = node.Element("AmendedResponse").Value,
        param46 = node.Element("AmendedResponseDescription").Value,
        param47 = node.Element("EmployerSepReasonComments").Value,
        param48 = node.Element("LaborDisputeTypeInd").Value,
        param49 = node.Element("AttachmentID").Value,
        param50 = node.Element("MandatoryRetirementInd").Value,
        param51 = node.Element("PriorIncidentID").Value,
        param52 = node.Element("PreparerTypeCode").Value,
        param53 = node.Element("PreparerCompanyName").Value,
        param54 = node.Element("PreparerTelephoneNumberPlusExt").Value,
        param55 = node.Element("PreparerContactName").Value,
        param56 = node.Element("PreparerTitle").Value,
        param57 = node.Element("PreparerFaxNbr").Value,
        param58 = node.Element("PreparerEmailAddress").Value,
    };



foreach (var node in EmployerTPASeparationResponse)
{
    cmd4.Parameters["StateRequestRecordGUID"].Value = node.param1;
    cmd4.Parameters["BrokerRecordTransactionNumber"].Value = node.param2;
    cmd4.Parameters["SSN"].Value = node.param3;
    cmd4.Parameters["ClaimEffectiveDate"].Value = node.param4;
    cmd4.Parameters["ClaimNumber"].Value = node.param5;
    cmd4.Parameters["StateEmployerAccountNbr"].Value = node.param6;
    cmd4.Parameters["CorrectedEmployerName"].Value = node.param7;
    cmd4.Parameters["CorrectedStateEmployerAccountNbr"].Value = node.param8;
    cmd4.Parameters["CorrectedFEIN"].Value = node.param9;
    cmd4.Parameters["OtherSSN"].Value = node.param10;
    cmd4.Parameters["ClaimantNameWorkedAsForEmployers"].Value = node.param11;
    cmd4.Parameters["ClaimantJobTitle"].Value = node.param12;
    cmd4.Parameters["SeasonalEmploymentInd"].Value = node.param13;
    cmd4.Parameters["EmployerReportedClaimantFirstDayofWork"].Value = node.param14;
    cmd4.Parameters["EmployerReportedClaimantLastDayofWork"].Value = node.param15;
    cmd4.Parameters["EffectiveSeparationDate"].Value = node.param16;
    cmd4.Parameters["TotalEarnedWagesNeededInd"].Value = node.param17;
    cmd4.Parameters["TotalEarnedWages"].Value = node.param18;
    cmd4.Parameters["TotalWeeksWorkedNeededInd"].Value = node.param19;
    cmd4.Parameters["TotalWeeksWorked"].Value = node.param20;
    cmd4.Parameters["WagesEarnedAfterClaimEffectiveDate"].Value = node.param21;
    cmd4.Parameters["NumberOfHoursWorkedAfterClaimEffectiveDate"].Value = node.param22;
    cmd4.Parameters["AverageWeeklyWage"].Value = node.param23;
    cmd4.Parameters["EmployerSepReasonCode"].Value = node.param24;
    cmd4.Parameters["ReturnToWorkInd"].Value = node.param25;
    cmd4.Parameters["ReturnToWorkDate"].Value = node.param26;
    cmd4.Parameters["WorkingAllAvailableHoursInd"].Value = node.param27;
    cmd4.Parameters["NotWorkingAvailableHoursReason"].Value = node.param28;
    cmd4.Parameters["DischargeReasonCode"].Value = node.param29;
    cmd4.Parameters["FinalIncidentReason"].Value = node.param30;
    cmd4.Parameters["FinalIncidentDate"].Value = node.param31;
    cmd4.Parameters["ViolateCompanyPolicyInd"].Value = node.param32;
    cmd4.Parameters["DischargePolicyAwareInd"].Value = node.param33;
    cmd4.Parameters["DischargePolicyAwareExplanationCode"].Value = node.param34;
    cmd4.Parameters["WhoDischargedName"].Value = node.param35;
    cmd4.Parameters["WhoDischargedTitle"].Value = node.param36;
    cmd4.Parameters["DischargeReasonComments"].Value = node.param37;
    cmd4.Parameters["VoluntarySepReasonCode"].Value = node.param38;
    cmd4.Parameters["HiringAgreementChangesCode"].Value = node.param39;
    cmd4.Parameters["HiringAgreementChangeComments"].Value = node.param40;
    cmd4.Parameters["ClaimantActionstoAvoidQuitInd"].Value = node.param41;
    cmd4.Parameters["ActionTakenComments"].Value = node.param42;
    cmd4.Parameters["ContinuingWorkAvailableInd"].Value = node.param43;
    cmd4.Parameters["VoluntarySepReasonComments"].Value = node.param44;
    cmd4.Parameters["AmendedResponse"].Value = node.param45;
    cmd4.Parameters["AmendedResponseDescription"].Value = node.param46;
    cmd4.Parameters["EmployerSepReasonComments"].Value = node.param47;
    cmd4.Parameters["LaborDisputeTypeInd"].Value = node.param48;
    cmd4.Parameters["AttachmentID"].Value = node.param49;
    cmd4.Parameters["MandatoryRetirementInd"].Value = node.param50;
    cmd4.Parameters["PriorIncidentID"].Value = node.param51;
    cmd4.Parameters["PreparerTypeCode"].Value = node.param52;
    cmd4.Parameters["PreparerCompanyName"].Value = node.param53;
    cmd4.Parameters["PreparerTelephoneNumberPlusExt"].Value = node.param54;
    cmd4.Parameters["PreparerContactName"].Value = node.param55;
    cmd4.Parameters["PreparerTitle"].Value = node.param56;
    cmd4.Parameters["PreparerFaxNbr"].Value = node.param57;
    cmd4.Parameters["PreparerEmailAddress"].Value = node.param58;
    cmd4.ExecuteNonQuery();
    if (cmd4 != null)
    {
        cmd4 = null;
    }
}


You need to include the namespace (xmlns) in all of the element names:

XNamespace ns = "https://uidataexchange.org/schemas";

doc.Descendants(ns + "EmployerTPASeparationResponse")

(And in all of the Element calls)

Note that your code can be made dramatically shorter by looping through the child elements and adding parameters with their names instead of using anonymous types.

For example:

var node = doc.Descendants(ns + "EmployerTPASeparationResponse").Single();

using (cmd4) {
    foreach(var param in node.Elements()) {
        cmd4.Parameters.AddWithValue(param.Name.LocalName, param.Value);
    }
    cmd4.ExecuteNonQuery();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜