2011-05-12 53 views
0

我有一些代碼使用以下xml文件。有人能告訴我爲什麼我的doc.Descendants返回null嗎?c#:使用Linq獲取一個IEnumerable XML節點集合來遍歷

下面是XML文件:

<?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>98761987654321323456109883</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>[email protected]</PreparerEmailAddress> 
    </EmployerTPASeparationResponse> 
    <EmployerTPASeparationResponse> 
    <StateRequestRecordGUID>98761987654321323456109884</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</PreparerTitle> 
    <PreparerFaxNbr>5555555555</PreparerFaxNbr> 
    <PreparerEmailAddress>[email protected]</PreparerEmailAddress> 
    </EmployerTPASeparationResponse> 
</EmployerTPASeparationResponseCollection> 

這裏是我使用的代碼。我首先使用一些Linq來獲取匿名值,然後嘗試遍歷每個子節點並使用cmd.parameters填充。

下面是代碼:

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; 
    } 
} 
+2

你應該在你的匿名類型更好的名字。 – SLaks 2011-05-12 13:45:30

回答

3

您需要包括命名空間(xmlns)中的所有元素名稱:

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

doc.Descendants(ns + "EmployerTPASeparationResponse") 

(以及所有Element電話)

請注意,通過循環子元素和使用名稱添加參數而不是使用匿名類型,可以使代碼縮短得更短。

例如:

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

是否有一個命名空間,我需要包括爲了獲得param.LocalName選項? – Zach 2011-05-12 14:04:08

+0

@Zach:對不起,我的意思是'.Name.LocalName'。 – SLaks 2011-05-12 14:05:54

+0

@Slaks:對於param.Value,我得到一個錯誤,無法從system.data.sqlclient.sqlparameter – Zach 2011-05-12 14:09:47