使用xelement将结果添加到字典中


private Dictionary<int, double> TaxDiction { set; get; }     
XDocument doc = XDocument.Load(path);
XElement xelement = XElement.Load(path);
var query = from nm in xelement.Descendants("EmployeeFinance")
            where (int)nm.Element("EmpPersonal_Id") == empID
            select new AllowancePaid
            {
                TaxDiction = ??
            };
var resultquery = query.SingleOrDefault();

编辑我正在从xml文件中进行选择,并希望将值插入字典(TaxDiction)中。然后选择两个值并将其插入到单独的列表中,如下所示:

list1 = nm.Element("ListOfTaxableAllowance").Elements("Amount").Attributes("BenListId").Select(a => (int)a).ToList(),
list2 = nm.Element("ListOfTaxableAllowance").Elements("Amount").Select(a => (double)a).ToList()

然而,两个列表中的值是相关的,我想将它们插入到字典中。所以list1中的值将是键,list2中的值将是字典中的值。我认为这样会更有效率,因为值是相关的。我希望这能帮你弄清楚一点。谢谢你!

编辑

我试过…

TaxDiction.Add(nm.Element("ListOfTaxableAllowance").Elements("Amount").Attributes("BenListId").Select(a => (int)a).ToList(),nm.Element("ListOfTaxableAllowance").Elements("Amount").Attributes("BenListId").Select(a => (int)a).ToList())

然而,我得到了一个无效的初始化成员声明符

XML示例文件

  <EmployeeFinance>
    <EmpPersonal_Id>494</EmpPersonal_Id>
    <NonStatDedct>
      <DeductedAmt NonStatID="1037">0</DeductedAmt>
      <DeductedAmt NonStatID="106">5000</DeductedAmt>
    </NonStatDedct>
    <TotalDeduction>39909.83</TotalDeduction>
    <TotalTaxableEarnings>120054.27</TotalTaxableEarnings>
    <TotalNonTaxableEarnings>29500</TotalNonTaxableEarnings>
    <No_DaysWorked>21.667</No_DaysWorked>
    <Payperiod_EndDate>2014-02-28T00:00:00</Payperiod_EndDate>
    <Exchange_Rate>207.00</Exchange_Rate>
    <Currency>GYD</Currency>
    <Date_Appointment>2009-11-30T00:00:00</Date_Appointment>
    <Date_Employment>1994-12-01T00:00:00</Date_Employment>
    <Date_Termination>0001-01-01T00:00:00</Date_Termination>
    <Payperiod_StartDate>2014-02-01T00:00:00</Payperiod_StartDate>
    <BatchNumber>3192</BatchNumber>
    <PAYE_Free_Pay_Awarded>50000</PAYE_Free_Pay_Awarded>
    <Income_Tax_RateID>4</Income_Tax_RateID>
    <NIS_RateID>1</NIS_RateID>
    <Daily_Rate>5540.881</Daily_Rate>
    <NIS_weeks_worked>0</NIS_weeks_worked>
    <Incentive />
    <Retro>0</Retro>
    <ListOfTaxableAllowance>
      <Amount BenListId="4">0.00000</Amount>
      <Amount BenListId="0">0</Amount>
    </ListOfTaxableAllowance>
    <ListOfTNonaxableAllowance>
      <Amount BenListId="4">23500.00000</Amount>
      <Amount BenListId="0">0</Amount>
    </ListOfTNonaxableAllowance>
    <ListOfTaxableBenefits>
      <Amount BenListID="0">0</Amount>
    </ListOfTaxableBenefits>
    <ListOfNonTaxableBenefits>
      <Amount BenListID="0">0</Amount>
    </ListOfNonTaxableBenefits>
    <ListOfTaxableAddIncome>
      <Amount AddEarnID="14">0.00000</Amount>
    </ListOfTaxableAddIncome>
    <ListOfNonTaxableAddIncome>
      <Amount AddEarnID="14">6000.00000</Amount>
    </ListOfNonTaxableAddIncome>
    <ListOfNISdeductible>
      <Amount>0</Amount>
    </ListOfNISdeductible>
    <ListOfOtherTaxableAmount>
      <Amount>0</Amount>
    </ListOfOtherTaxableAmount>
    <ListOfPartofPension>
      <Amount>23500.00</Amount>
    </ListOfPartofPension>
  </EmployeeFinance>

我认为你需要用BenListId属性作为键,Amount作为值的字典:

TaxDiction = nm.Element("ListOfTaxableAllowance")
               .Elements("Amount")
               .ToDictionary(a => (int)a.Attribute("BenListId"),
                             a => (double)a)

相关内容

  • 没有找到相关文章