Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. need to get data from xml to verify login

need to get data from xml to verify login

Scheduled Pinned Locked Moved XML / XSL
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    faizych
    wrote on last edited by
    #1

    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"); } } } }

    L 1 Reply Last reply
    0
    • F faizych

      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"); } } } }

      L Offline
      L Offline
      Le centriste
      wrote on last edited by
      #2

      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".

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups