C# 将列表插入XML文件

C# 将列表插入XML文件,c#,xml,linq,C#,Xml,Linq,我创建了一个xml文件,如下所示,还有一个使用linq查询得到的元素列表。 public bool createXMLFile() { bool myval; try { var xEle = new XElement("Plist"); string path1 = AppDomain.CurrentDo

我创建了一个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(列表预列表)
{
Debug.WriteLine(“员工财务”);
Debug.WriteLine(“*******************************************************************************************************”);
var finalList=新列表();
var context=new SSPModel.sspenties();
foreach(预列表中的变量xEle)
{
var query=来自context.Employee\u Employee中的c
在context.Employee\u Financial中加入d
在c.Employee_Personal_InfoEmp_id等于d.Employee_Personal_InfoEmp_id
其中d.Employee\u Personal\u InfoEmp\u id==xEle.Employee\u Personal\u InfoEmp\u id/&&d.Approved\u on!=null
选择新员工财务
{
基本工资=d.基本工资,
住房津贴=d.住房津贴,
基本支付货币=d.基本支付货币,
Employee_Personal_InfoEmp_id=d.Employee_Personal_InfoEmp_id
};
var empfinance=query.SingleOrDefault();
if(query.Count()==1)
{
xEle.Housing_account=empfinance.Housing_account.GetValueOrDefault();
xEle.Base\u Pay=empfinance.Base\u Pay;
xEle.Base\u Pay\u Currency=empfinance.Base\u Pay\u Currency;
xEle.Gross_Pay=xEle.Housing_amount.GetValueOrDefault()+xEle.Base_Pay;//假设到目前为止都是总工资
添加(xEle);
}
}
Misc_Classes.XMLCreator wer=新的Misc_Classes.XMLCreator();
bool myVal=wer.createXMLFile();
如果(myVal==true)
{
insertinitialist(finalList);//将列表插入xml文件的方法调用。
}
其他的
{
Security.ErrorControl aa=新Security.ErrorControl();
aa.displayErrorMsg=“创建XML”;
aa.endRequest(HttpContext.Current,2);
Security.ErrorControl.CaptureErrorMessage(0,0,“我的val返回错误”,“BeginPayrollRunProcess”,GlobalClass.GlobalVar);
返回;
}
}
--

public void insertialist(List finalList)
{
XDocument doc=XDocument.Load(GlobalClass.GlobalUrl);
XElement XElement=XElement.Load(GlobalClass.GlobalUrl);
尝试
{
var result=来自文档子体(“Plist”)中的元素
选择元素;
foreach(结果中的变量)
{
元素(“Plist”)。添加(来自finalList中的emp
选择新XElement(“EmployeeFinance”,
新XElement(“雇员雇员Id”,emp.Id),
新XElement(“emp个人识别码”,emp.员工个人识别码),
新条款(“员工编号”,emp.员工编号),
新条款(“总薪酬”,皇帝总薪酬),
新元素(“等级Id”,皇帝等级Id),
新的元素(“位置Id”,emp.实质性名称),
新条款(“基本工资”,0),
新XElement(“住房”,emp.住房津贴.GetValueOrDefault()),
新条款(“基本工资”,emp.基本工资),
新XElement(“PAYE”,0),
新XElement(“网付”,0),
新XElement(“所有等级总和”,0),
新XElement(“总和位置”,0),
新XElement(“津贴等级”),
新XElement(“总免税额等级”,0),
新的XElement(“总免税额等级”,0),
新XElement(“余量位置”,0),
新XElement(“TotalTaxableAllowment Position”,0),
新的XElement(“总非税备抵位置”,0),
新XElement(“总非税备抵”),
新条款(“可征税免税额总额”),
新XElement(“加上收入和非税总额”,0),
新的XElement(“增加收入不可征税”),
新XElement(“加上收入和可征税总额”,0),
新项目(“增加应纳税收入”),
新条款(“福利税总额”,0),
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);
            }

        }
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);
            }



        }
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);
            }



        }