need to get data from xml to verify login
-
hi im doing a small project for friend.. i need to get the data out of xml so that i can verify the username and password xml is like this PLEASE HELP THE ERROR I GET IS EXPLAINED IN THE MIDDLE.. THANKS ALOTTTT Password="pass" /> Password="pass78" /> and to get the value im doing private void button1_Click(object sender, EventArgs e) { XmlDocument xmldoc = new XmlDocument(); xmldoc.Load("path.xml"); System.Xml.XmlNodeList client; XmlNode clientNode = xmldoc.DocumentElement; client = clientNode.SelectNodes("Client"); string Usernamttext = txtUsername.Text; string passwordtext = txtpassword.Text; foreach (XmlNode DataNode in client) { string userid; string password; ************************************************************************************************ ******* ERROR HERE*** now when i run it it gives me an error when it gets to the line below that """object reference not set to an instance of an object""" ************************************************************************************************* userid = DataNode.SelectSingleNode("Username").InnerText.Trim(); password = DataNode.SelectSingleNode("password").InnerText.Trim(); if (string.Compare(Usernamttext, userid) == 0) { if (string.Compare(passwordtext, password) == 0) { break; UserProfile UserProfileForm = new UserProfile(); UserProfileForm.usernamevalue = userid; UserProfileForm.Show(); this.Close(); } else { MessageBox.Show("Invalid Username or password"); } } } }
-
hi im doing a small project for friend.. i need to get the data out of xml so that i can verify the username and password xml is like this PLEASE HELP THE ERROR I GET IS EXPLAINED IN THE MIDDLE.. THANKS ALOTTTT Password="pass" /> Password="pass78" /> and to get the value im doing private void button1_Click(object sender, EventArgs e) { XmlDocument xmldoc = new XmlDocument(); xmldoc.Load("path.xml"); System.Xml.XmlNodeList client; XmlNode clientNode = xmldoc.DocumentElement; client = clientNode.SelectNodes("Client"); string Usernamttext = txtUsername.Text; string passwordtext = txtpassword.Text; foreach (XmlNode DataNode in client) { string userid; string password; ************************************************************************************************ ******* ERROR HERE*** now when i run it it gives me an error when it gets to the line below that """object reference not set to an instance of an object""" ************************************************************************************************* userid = DataNode.SelectSingleNode("Username").InnerText.Trim(); password = DataNode.SelectSingleNode("password").InnerText.Trim(); if (string.Compare(Usernamttext, userid) == 0) { if (string.Compare(passwordtext, password) == 0) { break; UserProfile UserProfileForm = new UserProfile(); UserProfileForm.usernamevalue = userid; UserProfileForm.Show(); this.Close(); } else { MessageBox.Show("Invalid Username or password"); } } } }
Your error has nothing to do with XML itself. The call to DataNode.SelectSingleNode probably returns null. In fact, directly calling methods on method calls that are prone to return null is one of the worst coding habit someone can have.Here is what a good programmer would have done:
userid = DataNode.SelectSingleNode("Username"); if ((userid != null) && (!String.IsNullOrEmpty(userid.InnerText)) { // Do your stuff } else { throw new NoUsernameNodeException(); }
Not checking for null values before using object references is one the worst thing to do, always leading to obscure error message "Object reference not set to an object".