以下に示すように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ファイルに挿入するために使用している方法に何か問題があることを知っています。いくつかのアイデアは大歓迎です。ありがとうございました
**編集済み
上記の問題の短縮版で..私はそれを理解し、以下のコードをリストします。ありがとうございました**
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);
}
}