Dineshp
Dineshp

Reputation: 31

Looping throught XML element to add data

I dont know how exactly to word my question, so apologies from up front. I have an xml file and it has elements like the following:

- <Allow_BenGrade>
  <Amount BenListID="0">0</Amount> 
  </Allow_BenGrade>
- <Add_Earnings_NonTaxable>
  <Amount AddEarnID="0">0</Amount> 
  </Add_Earnings_NonTaxable>

I am interested in Allow_BenGrade where i can add multiple elements inside there. I have list of 3 items but when I loop through to write it to the file, it only writes the last item in the list, so instead of have 3 elements inside Allow_BenGrade, i end up having one (last one in the item list). My code is below. Please help thank you.

var query = from nm in xelement.Elements("EmployeeFinance")
                                    select new Allowance {
                                    a_empersonalID = (int)nm.Element("EmpPersonal_Id"),
                                    a_allbengradeID = (int)nm.Element("Grade_Id")
                           };
                var x = query.ToList();


                foreach (var xEle in x)
                {
                    var qryBenListGrade = from ee in context.Employee_Employ
                                 join abg in context.All_Inc_Ben_Grade
                                 on ee.Grade_Id equals abg.GradeID
                                 join abl in context.All_Inc_Ben_Listing
                                 on abg.All_Inc_Ben_ListingID equals abl.ID
                                 where ee.Employee_Personal_InfoEmp_id == xEle.a_empersonalID && abg.GradeID == xEle.a_allbengradeID && (abl.Part_of_basic == "N" && abl.Status == "A" && abl.Type_of_earnings == 2)
                                 //abl.Approved_on !=null &&
                                 select new 
                                 {
                                        abl.ID,
                                        abl.Amount,
                                        abg.GradeID,
                                        ee.Employee_Personal_InfoEmp_id,
                                        abl.Per_Non_Taxable,
                                        abl.Per_Taxable
                                 };
                    var y = qryBenListGrade.ToList();
                    //xEle.a_Amount = 0;
                    foreach (var tt in y)
                    {
                   Debug.WriteLine("amount: " + tt.Amount + " emp id: " + tt.Employee_Personal_InfoEmp_id + " ben list id: " + tt.ID);
                       // xEle.a_Amount = xEle.a_Amount + tt.Amount;

                        var result = from element in doc.Descendants("EmployeeFinance")
                                     where int.Parse(element.Element("EmpPersonal_Id").Value) == tt.Employee_Personal_InfoEmp_id
                                     select element;
                        foreach (var ele in result)
                        {
                            ele.Element("Allow_BenGrade").SetElementValue("Amount", tt.Amount);
                            //ele.Element("Allow_BenGrade").Element("Amount").SetAttributeValue("BenListID", tt.ID);
                        }

                    }
                    doc.Save(GlobalClass.GlobalUrl);
                }

Upvotes: 0

Views: 152

Answers (2)

Alex Filipovici
Alex Filipovici

Reputation: 32561

The XElement.SetElementValue Method:

Sets the value of a child element, adds a child element, or removes a child element.

Also:

The value is assigned to the first child element with the specified name. If no child element with the specified name exists, a new child element is added. If the value is null, the first child element with the specified name, if any, is deleted.

This method does not add child nodes or attributes to the specified child element.

You should use the XElement.Add Method instead.

Upvotes: 0

Tallmaris
Tallmaris

Reputation: 7590

SetElementValue will, as the name suggests, set the value of the Amount element... You need to Add a new one instead:

ele.Element("Allow_BenGrade").Add(new XElement("Amount", 
                                      new XAttribute("BenListID", tt.ID),
                                      tt.Amount);

Let me know if that solves it for you.

Upvotes: 2

Related Questions