Linq to xml通过xelement更新

时间:2013-02-26 16:43:18

标签: asp.net xml linq-to-xml

我有一个xml文档,我想使用linq to xml将totalGrade更新为列表中的值。任何想法?

<?xml version="1.0" encoding="utf-8"?>
<EmpFinanceList>
  <EmployeeFinance>
    <EmpEmployId>122</EmpEmployId>
    <EmpPersonalId>23000</EmpPersonalId>
    <dateofApp>2011-10-31T00:00:00</dateofApp>
    <dateofEmpl>2013-02-15T00:00:00</dateofEmpl>
    <dateofTerm />
    <grade_id>73</grade_id>
    <position_id>2065</position_id>
    <startDate>2013-02-14T00:00:00</startDate>
    <endDate>2013-02-21T00:00:00</endDate>
    <Exchange_rate>200</Exchange_rate>
    <housing>45000</housing>
    <base_pay>150000</base_pay>
    <totalGrade>0</totalGrade>
    <toalPosition>0</toalPosition>
  </EmployeeFinance>

EDITED -------------

这是我创建我的xml文件的方式,请告诉我是否错误以及该怎么做

   private static void getEmployeeForEmpFinance(List<BasicPay> EmployeeEmployList)
        {
            var employeeFinance = new List<BasicPay>();
            var context = new SSPModel.sspEntities();
            for (int i = 0; i < 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<BasicPay>();
            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 && abl.Part_of_basic == "Y" && 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 < totalGrade.Count(); i++)
            {
                Debug.WriteLine(totalGrade.ElementAt(i).employee_personal_id);
                xelement.Elements("EmployeeFinance").Where(d => int.Parse(d.Attribute("EmpPersonalId").Value) == totalGrade.ElementAt(i).employee_personal_id).SingleOrDefault().SetAttributeValue("totalGrade", totalGrade.ElementAt(i).totalGrade);
            }

        }

2 个答案:

答案 0 :(得分:1)

首先,您必须加载该文档:

var doc = XDocument.Load(filePath);

之后,您将能够更改<totalGrade>元素值:

doc.Root.Element("EmployeeFinance")
        .SetElementValue("totalGrade", newValue);

<强>更新

如果您在生成xml时尝试设置该值,请更改该行:

                               new XElement("totalGrade", 0),

0更改为列表商品属性/字段:

                               new XElement("totalGrade", emp.totalGrade),

答案 1 :(得分:0)

XDocument doc = XDocument.Load(path);
        var result = from element in doc.Descendants("EmployeeFinance")
                     select element;
        foreach (var ele in result)
        {  
            ele.SetElementValue("totalGrade", newvalue);

        }
        doc.Save(path);
  1. 加载文档
  2. 选择结果
  3. 遍历每个xml块(如果您有多条记录)
  4. 设置新值
  5. 保存xml文档
相关问题