Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Basically i have a piece of code which i created, which is a if loop (selection) and i want it to search a XML file for what has been entered in the textboxes, but it only searches the first node and then either declares it maches or not. How do i make it please search the whole file and look for matches and not the first node. Below is the code i have at the moment.

C#
if (UsernameTextbox.Text == UsernamenodeVariable && PasswordTextbox.Text == PasswordnodeVariable && PermissionComboBox.SelectedItem.ToString() == PermissionnodeVariable)
{

    MessageBox.Show("Login Details Match");
    Menu MenuPage = new Menu();
    MenuPage.Show();
    this.Hide();
}



Any suggestions please for this. Would be greatful
Posted
Updated 9-Dec-13 5:58am
v4
Comments
Mahesh Bailwal 8-Dec-13 9:57am    
can you share sample XML file and example of what you want to search in that.
Member 9665875 8-Dec-13 11:22am    
XmlNode Usernamenode = xdoc.SelectSingleNode("Users/User/Username");
string UsernamenodeVariable = Usernamenode.InnerText;

XmlNode Passwordnode = xdoc.SelectSingleNode("Users/User/Password");
string PasswordnodeVariable = Passwordnode.InnerText;

XmlNode Permissionnode = xdoc.SelectSingleNode("Users/User/Permission");
string PermissionnodeVariable = Permissionnode.InnerText;
Richard MacCutchan 8-Dec-13 12:37pm    
You should use your debugger to check the actual values of each variable at the time of the if statement, to determine why it always matches.

1 solution

Hi.

The reason it searches only the first node is because you need to specify in which node to search. Currently you only specify the upper most node sets with
xdoc.SelectSingleNode("Users/User/Password");


to search for subnodes, you need to use
subnode.SelectSingleNode("Users/User/Password");


If you know the names of the nodes, this becomes easy. If you don't, this becomes a bit more tricky. I have a recursive method and an override for it that could help you. It's not the most elegant code there is but it does solve your problem.

C#
private XmlNode checkNode(XmlDocument xdoc, string nodeName)
{
   XmlNode node;
   if(xdoc.SelectSingleNode(nodeName) != null)
   {
      node = xdoc.SelectSingleNode(nodeName);
   }
   else
   {
      foreach(XmlNode currentNode in xdoc.DocumentElement.ChildNodes)
      {
         node = checkNode(currentNode, nodeName);
         if(node != null)
         {
             break;
         }
      }
   }
   return node;
}

private XmlNode checkNode(XmlNode parentNode, string nodeName)
{
   XmlNode node;
   if(currentNode.SelectSingleNode(nodeName) != null)
   {
      node = xdoc.SelectSingleNode(nodeName);
   }
   else
   {
      foreach(XmlNode currentNode in parentNode.DocumentElement.ChildNodes)
      {
         node = checkNode(currentNode, nodeName);
         if(node != null)
         {
             break;
         }
      }
   }
   return node;
}


Here is how you would do the check (Example):

//So it's easier to read
string username = UsernameTextbox.Text;
string password = PasswordTextbox.Text;
string permission = PermissionComboBox.SelectedItem.ToString();

if (username == checkNode(xdoc, "usernameNodeName").InnerText
    && password == checkNode(xdoc, "passwordNodeName").InnerText
    && permission == checkNode(xdoc, "permissionNodeName").InnerText)
{
   MessageBox.Show("Login Details Match");
   Menu MenuPage = new Menu();
   MenuPage.Show();
   this.Hide();
}
 
Share this answer
 
v6
Comments
Member 9665875 9-Dec-13 11:52am    
This sounds very good perhaps i should explain a bit more though. So what i am wanting is a piece of code that when checked against a textbox it searches for the nodes say for example "Users/User/Password" checks what is in the "Password" node for each "User" and goes down each node looking for what is been entered in the textbox. Each "User" has a block of ChildNodes "UsernameNode" & "PasswordNode" & "PermissionNode". The aim of my code is to check against all three textboxes and if all is equal open a form. Any more further suggestions
Member 7722275 10-Dec-13 2:23am    
I've updated the solution.

I apologize if I am doing the work for you. Figuring out how to do these things is part of the learning process.

The method now returns your desired node. Simply assign the returned node and then do what you like with it.

Like I said, this might not be the best way to do it, but it does solve your problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900