Reputation: 1512
How would I go about editing this XML file:
<?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>
So far I have the following to select the correct employee from the 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?
}
I'm guessing I've found the right place to insert the editing for the file but then I need to know how to correctly edit it and then save it to the file without losing everything else contained there (otherwise I'd just overwrite the whole file.
Thanks for any help.
Upvotes: 0
Views: 6317
Reputation: 699
A good idea is also to use the xml-serializer-class.
There you can work with usual objects and save it to the xml later :)
http://msdn.microsoft.com/en-us/library/ms733901.aspx
Upvotes: 2
Reputation: 2514
Here is a useful link for updating an XML file without rewriting it in its entirety on each save: http://support.microsoft.com/kb/301233
the general idea is to use a XMLDocument instead of a XMLTextReader and load your .xml file into the XMLDocument object. then grab the root node and start navigating/querying. once you have found the nodes you wish to edit, make your changes. then when you are done, use XMLDocument.Save(path) to save your chagnes.
IO streams are one way phenoma so you can;t use an reader/writer unless you want to overwrite the existing file.
Upvotes: 1