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>
編集 -------------
これは私が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);
}
}