1

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 ?

5
  • If you can't choose where to place button, I suggest to place it at the right side of textbox Commented Feb 4, 2014 at 16:53
  • What I want is to search in the xml, according to the text in the textbox, and after finding a certain node <name> in the <employee>, change the value from the enter node, and add new node with the current time and date into the node. I write for instance Name1, and the value of Enter is changed from 0 to 1, and in the same time it adds a new <data> node in the <log> node Commented Feb 4, 2014 at 16:58
  • @user2962759 what exactly gives you problem? Reading value from UI? Parsing xml? Updating value in xml? Show your code Commented Feb 4, 2014 at 17:06
  • I am new into the xml, and I really don't know how to start, it's ok reading and saving the xml, getting the inner text should be something like string xPathString = String.Format("/Employees/Employee/Name[.=\"{0}\"]]", texbox1.Text); So I appreciate any help Commented Feb 4, 2014 at 17:11
  • After that, how can I find the Enter node, and change it's innerText, then saving it into a new <data> node in <log> Commented Feb 4, 2014 at 17:13

1 Answer 1

1

It will make it easier to search the data if you make the node easier to use. I suggest something like:

<data Time="02.04.2014 13:00:00" Enter="1" />

Then you can search for that time and enter value properly, using Linq-to-XML

XElement root = XElement.Load(file); // .Parse(string)
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);

To change the values, you then do:

data.Attribute("Time").Value = time.ToString();
data.Attribute("Enter").Value = 0.ToString();

Then save the changes with:

root.Save(file);

There are whole works on how to create XElements with XAttributes for the attributes, be sure to look them up with any search engine.

Sign up to request clarification or add additional context in comments.

4 Comments

this is fine, now, how can i search get into the node whose name is entered in a textbox? and then change the values in the <enter> and add new <data> node?
XElement root = XElement.Load("data.xml"); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("data.xml"); string xPathString = String.Format("/Employees/Employee/Name[.=\"{0}\"]", textBox1.Text); XmlNodeList nodeList = xmlDoc.SelectNodes(xPathString); foreach (XmlNode node in nodeList) { } Do I have to use XElement and XmlDocument, because SelectNodes is used in XmlDocument? Is this right?
The question is, how can i get into the node where <name> node equals textbox.text? and then change value into <enter> and add new <data> child in <log> ?
@user2962759 You need to research more on using Linq-to-xml. The XPath expression you have is ok, but there are generally better ways using Linq-to-xml. You can use XPath with Linq-to-xml if you must by including System.Xml.XPath

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.