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

时间:2014-03-11 12:39:22

标签: c# xml linq dictionary

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中的值将是我的字典中的值。我认为这样的效率更高,因为价值观是相关的。我希望这有助于澄清一些事情。谢谢。

EDITED

我试过......

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>

1 个答案:

答案 0 :(得分:3)

我认为您需要使用BenListId属性作为键的字典,并将Amount作为值:

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