我将如何编辑这个 XML 文件:
<?xml version="1.0" encoding="utf-8" ?>
<employees>
<employee id="657434365436543" name="Joe Bloggs" group="Manager" subgroup="Deputy">
<contactDetails>
<homePhone>6535436543</homePhone>
<mobilePhone>654365436543</mobilePhone>
</contactDetails>
<personelFile>
<rightToWork>
<type>Permanent</type>
<expires>Never</expires>
</rightToWork>
<nationalInsurance>6543655543</nationalInsurance>
<startDate>01/09/2009</startDate>
</personelFile>
<holidays>
<entitlements>
<holidays>22</holidays>
<bankHolidays>8</bankHolidays>
<personalDays>1</personalDays>
</entitlements>
<taken>
<holidays>1</holidays>
<bankHolidays>0</bankHolidays>
<personalDays>0</personalDays>
</taken>
<remaining>
<holidays>21</holidays>
<bankHolidays>8</bankHolidays>
<personalDays>1</personalDays>
</remaining>
<booked>
<holidays>22</holidays>
<bankHolidays>8</bankHolidays>
<personalDays>1</personalDays>
</booked>
<remainingtobook>
<holidays>0</holidays>
<bankHolidays>0</bankHolidays>
<personalDays>0</personalDays>
</remainingtobook>
</holidays>
<shifts>
<monday>
<start>0800</start>
<end>1300</end>
</monday>
<tuesday>
<start>0800</start>
<end>1300</end>
</tuesday>
<wednesday>
<start>0800</start>
<end>1300</end>
</wednesday>
<thursday>
<start></start>
<end></end>
</thursday>
<friday>
<start>0800</start>
<end>1300</end>
</friday>
<saturday>
<start>0800</start>
<end>1200</end>
</saturday>
<sunday>
<start></start>
<end></end>
</sunday>
</shifts>
</employee>
</employees>
到目前为止,我有以下内容可以从 XML 中选择正确的员工:
XmlTextReader employeesReader = new XmlTextReader("Employees.xml");
var employeesXdoc = XDocument.Load(employeesReader);
var employees = from employee in employeesXdoc.Descendants("employee")
where employee.Attribute("id").Value.ToString() == employeeSelect.Value.ToString()
select new
{
nodes = employee.Nodes()
};
foreach (var employee in employees)
{
// WHAT TO PUT HERE?
}
我猜我已经找到了插入文件编辑的正确位置,但是我需要知道如何正确编辑它,然后将其保存到文件中而不会丢失其中包含的所有其他内容(否则我只会覆盖整个文件。
谢谢你的帮助。