title: Linq to xml update via xelement
body: <p>I have an xml document and i would like to use linq to xml to update totalGrade to values i have in a list. any ideas?</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;EmpFinanceList&gt;
    &lt;EmployeeFinance&gt;
      &lt;EmpEmployId&gt;122&lt;/EmpEmployId&gt;
      &lt;EmpPersonalId&gt;23000&lt;/EmpPersonalId&gt;
      &lt;dateofApp&gt;2011-10-31T00:00:00&lt;/dateofApp&gt;
      &lt;dateofEmpl&gt;2013-02-15T00:00:00&lt;/dateofEmpl&gt;
      &lt;dateofTerm /&gt;
      &lt;grade_id&gt;73&lt;/grade_id&gt;
      &lt;position_id&gt;2065&lt;/position_id&gt;
      &lt;startDate&gt;2013-02-14T00:00:00&lt;/startDate&gt;
      &lt;endDate&gt;2013-02-21T00:00:00&lt;/endDate&gt;
      &lt;Exchange_rate&gt;200&lt;/Exchange_rate&gt;
      &lt;housing&gt;45000&lt;/housing&gt;
      &lt;base_pay&gt;150000&lt;/base_pay&gt;
      &lt;totalGrade&gt;0&lt;/totalGrade&gt;
      &lt;toalPosition&gt;0&lt;/toalPosition&gt;
    &lt;/EmployeeFinance&gt;
  </code></pre>
  
  <p>EDITED -------------</p>
  
  <p>this is how i created my xml file, please tell me if im wrong and what to do</p>
  
  <pre><code>   private static void getEmployeeForEmpFinance(List&lt;BasicPay&gt; EmployeeEmployList)
          {
              var employeeFinance = new List&lt;BasicPay&gt;();
              var context = new SSPModel.sspEntities();
              for (int i = 0; i &lt; EmployeeEmployList.Count; i++)
              {
                      int empID = EmployeeEmployList.ElementAt(i).employee_personal_id;
                      //var dateOfTermination = EmployeeEmployList.ElementAt(i).dateofTerm;
                      // Debug.WriteLine(dateOfTermination);
                      var query = from c in context.Employee_Financial
                                  where c.Employee_Personal_InfoEmp_id == empID 
                                  //orderby c.Employee_Personal_InfoEmp_id ascending
                                  select new BasicPay
                                  {
                                    base_pay=  c.Base_Pay,
                                    housing=  c.Housing_Allowance,
                                    Base_pay_currency = c.Base_Pay_Currency
                                  };
                      var empfinance = query.ToList();
                      foreach (var x in empfinance)
                      {
                          EmployeeEmployList.ElementAt(i).housing = x.housing;
                          EmployeeEmployList.ElementAt(i).base_pay = x.base_pay;
                      }                                    
              }
  
             try
              {
                              var xEle = new XElement("EmpFinanceList",
                              from emp in EmployeeEmployList
                              select new XElement("EmployeeFinance",
                                           new XElement("EmpEmployId", emp.emp_employID),
                                             new XElement("EmpPersonalId", emp.employee_personal_id),
                                             new XElement("dateofApp", emp.dateofApp),
                                             new XElement("dateofEmpl", emp.dateofEmpl),
                                             new XElement("dateofTerm", emp.dateofTerm),
                                             new XElement("grade_id", emp.grade_id),
                                             new XElement("position_id", emp.position_id),
                                             new XElement("startDate", emp.startDate),
                                             new XElement("endDate", emp.endDate),
                                             new XElement("Exchange_rate", emp.Exchange_rate),
                                             new XElement("housing", emp.housing),
                                             new XElement("base_pay", emp.base_pay),
                                             new XElement("totalGrade", 0),
                                             new XElement("toalPosition",0)
                                         ));
                  string path1 = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
                  string un = "\\"+GlobalClass.GlobalVar + "empFinance.xml";
                  GlobalClass.GlobalUrl = path1 + un;
                  xEle.Save(GlobalClass.GlobalUrl);
                  Debug.WriteLine("Converted to XML " + GlobalClass.GlobalUrl);
              }
              catch (Exception ex)
              {
                  Debug.WriteLine(ex.Message);
              }
             getFromAllBenGrade();
             //getFromAllBenGrade(EmployeeEmployList);
          }
         public static void getFromAllBenGrade()
          {
              var totalGrade = new List&lt;BasicPay&gt;();
              var context = new SSPModel.sspEntities();
              XElement xelement = XElement.Load(GlobalClass.GlobalUrl);
  
              var query = from nm in xelement.Elements("EmployeeFinance")
                          select new BasicPay
                          {
                              employee_personal_id = (int)nm.Element("EmpPersonalId"),
                              grade_id = (int)nm.Element("grade_id"),
                              totalGrade = (int)nm.Element("totalGrade")
                          };
              var x = query.ToList();
  
              Debug.WriteLine("Taken from xml:");
              foreach (var xEle in x)
              {
                  var query1 = 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.employee_personal_id &amp;&amp; abl.Part_of_basic == "Y" &amp;&amp; abl.Status == "A"
                               select new BasicPay
                               {
                                   all_ben_listID = abl.ID,
                                   amount = abl.Amount
  
                               };
                  var y = query1.ToList();
                  int? calculation;
                  int? total = 0;
                  foreach (var xyz in y)
                  {
                      calculation = xyz.amount;
                      total = total + calculation;
  
                  }
                  xEle.totalGrade = total;
                 // totalGrade.Add(xEle);
  
                  //var doc = XDocument.Load(GlobalClass.GlobalUrl);
                  //var hhhh = doc.Root.Element("EmployeeFinance")
                  //                    .SetElementValue("totalGrade", total);
  
              }
  
  
              for (int i = 0; i &lt; totalGrade.Count(); i++)
              {
                  Debug.WriteLine(totalGrade.ElementAt(i).employee_personal_id);
                  xelement.Elements("EmployeeFinance").Where(d =&gt; int.Parse(d.Attribute("EmpPersonalId").Value) == totalGrade.ElementAt(i).employee_personal_id).SingleOrDefault().SetAttributeValue("totalGrade", totalGrade.ElementAt(i).totalGrade);
              }
  
          }
  </code></pre>