将列表插入XML文件

时间:2013-05-22 16:45:04

标签: c# xml linq

我创建了一个xml文件,如下所示,我有一个使用linq查询获得的元素列表。

    public bool createXMLFile()
            {
                bool myval;
                try
                {
                var xEle = new XElement("Plist");
                string path1 = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
                string un = "\\" + GlobalClass.GlobalVar + "empFinance.xml";
                GlobalClass.GlobalUrl = path1 + un;
                xEle.Save(GlobalClass.GlobalUrl);
                    Debug.WriteLine("*************************************************************************");
                    Debug.WriteLine("Success! Converted to XML " + GlobalClass.GlobalUrl);
                    myval = true;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("**********************************************************************");
                    Debug.WriteLine("EmployeeForEmpFinance exception" + ex.Message);
                    myval = false;
                }
                return myval;  
            }

public void BeginPayrollRunProcess(List<EmployeeEmploy> PrepList)
    {
        Debug.WriteLine("EmployeeFinance");
        Debug.WriteLine("*****************************************************************");

        var finalList = new List<EmployeeEmploy>();
        var context = new SSPModel.sspEntities();


        foreach (var xEle in PrepList)
        {
           var query = from c in context.Employee_Employ
                        join d in context.Employee_Financial
                        on c.Employee_Personal_InfoEmp_id equals             d.Employee_Personal_InfoEmp_id
                        where d.Employee_Personal_InfoEmp_id == xEle.Employee_Personal_InfoEmp_id //&& d.Approved_on != null
                        select new EmployeeFinance
                        {
                            Base_Pay = d.Base_Pay,
                            Housing_Allowance = d.Housing_Allowance,
                            Base_Pay_Currency = d.Base_Pay_Currency,
                            Employee_Personal_InfoEmp_id = d.Employee_Personal_InfoEmp_id
                        };
            var empfinance = query.SingleOrDefault();

            if (query.Count() == 1)
            {
                xEle.Housing_Allowance = empfinance.Housing_Allowance.GetValueOrDefault();
                xEle.Base_Pay = empfinance.Base_Pay;
                xEle.Base_Pay_Currency = empfinance.Base_Pay_Currency;
                xEle.Gross_Pay = xEle.Housing_Allowance.GetValueOrDefault() + xEle.Base_Pay; // suppose to get gross pay up to this point
                finalList.Add(xEle);
            }

        }
        Misc_Classes.XMLCreator wer = new Misc_Classes.XMLCreator();
        bool myVal = wer.createXMLFile();

        if (myVal == true)
        {
            insertInitialList(finalList); // method call to insert list into xml file.
        }
        else
        {
            Security.ErrorControl aa = new Security.ErrorControl();
            aa.displayErrorMsg = " Creating XML";
            aa.endRequest(HttpContext.Current, 2);
            Security.ErrorControl.CaptureErrorMessage(0, 0, "my val return false", "BeginPayrollRunProcess", GlobalClass.GlobalVar);
            return;
        }
    }

-

public void insertInitialList(List<EmployeeEmploy> finalList)
        {
            XDocument doc = XDocument.Load(GlobalClass.GlobalUrl);
            XElement xelement = XElement.Load(GlobalClass.GlobalUrl);

            try
            {

                var result = from element in doc.Descendants("Plist")
                             select element;
                foreach (var ele in result)
                {

                    ele.Element("Plist").Add(from emp in finalList
                                                         select new XElement("EmployeeFinance",
                                                                        new XElement("EmployeeEmploy_Id", emp.ID),
                                                                        new XElement("EmpPersonal_Id", emp.Employee_Personal_InfoEmp_id),
                                                                        new XElement("Employee_Number", emp.Employee_No),
                                                                        new XElement("Gross_Pay", emp.Gross_Pay),
                                                                        new XElement("Grade_Id", emp.Grade_Id),
                                                                        new XElement("Position_Id", emp.Substantive_designation),
                                                                        new XElement("Basic_Pay", 0),
                                                                        new XElement("Housing", emp.Housing_Allowance.GetValueOrDefault()),
                                                                        new XElement("Base_Pay", emp.Base_Pay),
                                                                        new XElement("PAYE", 0),
                                                                        new XElement("NetPay", 0),
                                                                        new XElement("Sum_AllGrade", 0),
                                                                        new XElement("Sum_AllPosition", 0),
                                                                        new XElement("Allowance-Grade"),
                                                                        new XElement("TotalTaxableAllowance-Grade", 0),
                                                                        new XElement("TotalNonTaxableAllowance-Grade", 0),
                                                                        new XElement("Allowance-Position", 0),
                                                                        new XElement("TotalTaxableAllowance-Position", 0),
                                                                        new XElement("TotalNonTaxableAllowance-Position", 0),
                                                                        new XElement("TotalNonTaxableAllowance"),
                                                                        new XElement("TotalTaxableAllowance"),
                                                                        new XElement("Add_Earnings_NonTaxableTotal", 0),
                                                                        new XElement("Add_Earnings_NonTaxable"),
                                                                        new XElement("Add_Earnings_TaxableTotal", 0),
                                                                        new XElement("Add_Earnings_Taxable"),
                                                                        new XElement("Benefits_TaxableTotal", 0),
                                                                        new XElement("Benefits_Taxable"),
                                                                        new XElement("Benefits_NonTaxableTotal", 0),
                                                                        new XElement("Benefits_NonTaxable"),
                                                                        new XElement("NIS_Gross", 0),
                                                                        new XElement("CHMS"),
                                                                        new XElement("CHMSTotal"),
                                                                        new XElement("PensionEmp", 0),
                                                                        new XElement("PensionEmpr", 0),
                                                                        new XElement("NISEmp", 0),
                                                                        new XElement("NISEmpr", 0),
                                                                        new XElement("TotalNonStatDedct", 0),
                                                                        new XElement("NonStatDedct"),
                                                                        new XElement("TotalDeduction", 0),
                                                                        new XElement("TotalTaxableEarnings", 0),
                                                                        new XElement("TotalNonTaxableEarnings", 0),
                                                                        new XElement("No_DaysWorked", emp.NoDaysWorked),
                                                                        new XElement("Payperiod_EndDate", emp.endDate.ToShortDateString()),
                                                                        new XElement("Exchange_Rate", emp.Exchange_rate),
                                                                        new XElement("Currency", emp.Base_Pay_Currency),
                                                                        new XElement("Date_Appointment", emp.Date_of_Appointment.GetValueOrDefault().ToShortDateString()),
                                                                        new XElement("Date_Employment", emp.Date_of_Employment.ToShortDateString()),
                                                                        new XElement("Date_Termination", emp.Termination_date_actual.GetValueOrDefault().ToShortDateString()),
                                                                        new XElement("Payperiod_StartDate", emp.startDate.ToShortDateString()),
                                                                        new XElement("BatchNumber", emp.BatchNumber),
                                                                        new XElement("PAYE_ToDate_Computed", 0),
                                                                        new XElement("Income_Tax_RateID", 0),
                                                                        new XElement("NIS_RateID", 0),
                                                                        new XElement("NIS_weeks_worked", 0)
                                                                    ));
                     doc.Save(GlobalClass.GlobalUrl);
                }



            }
        catch (Exception e){
            Debug.WriteLine("********************************************************");
            Debug.WriteLine("EmployeeForEmpFinance exception" + e.Message);
            }

        }

在最后一个方法中,我试图迭代思考我的列表(finallist)并将其插入到我的xml文件中。我收到一个错误,说我需要实例化该对象。我知道我正在使用我用于将列表插入xml文件的方法出错。欢迎一些想法。谢谢

** EDITED


在上述问题的缩短版本中..我想出来并列出下面的代码。谢谢**

public void insertInitialList(List<EmployeeEmploy> finalList)
        {
            Debug.WriteLine("insertInitialList" + finalList.Count());

            XDocument doc = XDocument.Load(GlobalClass.GlobalUrl);

            var result = from element in doc.Descendants("Plist")
                         select element;

            foreach (var ele in result)
            {
                ele.Add(from a in finalList
                        select new XElement("EmployeeFinance",
                              new XElement("EmployeeEmploy_Id"),
                              new XElement("EmpPersonal_Id"),
                              new XElement("Employee_Number")));
                doc.Save(GlobalClass.GlobalUrl);
            }



        }

1 个答案:

答案 0 :(得分:0)

在上述问题的缩短版本中..我想出来并列出下面的代码。谢谢**

public void insertInitialList(List<EmployeeEmploy> finalList)
        {
            Debug.WriteLine("insertInitialList" + finalList.Count());

            XDocument doc = XDocument.Load(GlobalClass.GlobalUrl);

            var result = from element in doc.Descendants("Plist")
                         select element;

            foreach (var ele in result)
            {
                ele.Add(from a in finalList
                        select new XElement("EmployeeFinance",
                              new XElement("EmployeeEmploy_Id"),
                              new XElement("EmpPersonal_Id"),
                              new XElement("Employee_Number")));
                doc.Save(GlobalClass.GlobalUrl);
            }



        }