I'm creating an application where I want to search certain node, and change value in that root node. This is how my xml looks like:
<Employees>
<Employee>
<name>Name1</name>
<Enter>0</Enter>
<log>
<data Time="02.04.2014 13:00:00" Enter="1" />
<data Time="02.04.2014 15:00:00" Enter="0" />
</log>
</Employee>
<Employee>
<name>Name2</name>
<Enter>1</Enter>
<log>
<data Time="02.04.2014 11:00:00" Enter="1" />
<data Time="02.04.2014 12:00:00" Enter="0" />
<data Time="02.04.2014 13:00:00" Enter="1" />
</log>
</Employee>
</Employees>
So, I have a textbox, where I enter the name of the employee, for instance Name1. After clicking the button, the value of the tag is changing, from 0 to 1, and vice versa, and in the same tame it adds the time and date of logging in a new node, including the data from the enter field. The idea is a entry system, where you write the name, if a user is in or out, and in the same time it keeps a data as a personal log.
How can I search in the xml, according to the text in the textbox, and after finding a certain node, change the value from the enter node, and add new node with the current time and date into the node?
So far, this is my code:
XElement root = XElement.Load("data.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("data.xml");
string xPathString = String.Format("/Employees/Employees/name[.=\"{0}\"]", textBox1.Text);
XmlNodeList nodeList = xmlDoc.SelectNodes(xPathString);
foreach (XmlNode node in nodeList)
{
DateTime time = DateTime.Parse("02.04.2014 13:00:00");
XElement data = root.Descendants("data")
.FirstOrDefault(d => (DateTime)d.Attribute("Time") == time &&
(int)d.Attribute("Enter") == 1);
data.Attribute("Time").Value = time.ToString();
data.Attribute("Enter").Value = 0.ToString();
root.Save("data.xml");
}
xmlDoc.Save("data.xml");
Do I have to use XElement and XmlDocument ? Because SelectNodes goes with XmlDocument, don't know any other way. The question is, how can i get into the node where node equals textbox.text? and then change value into and add new child in ?