-1

So, i'm trying to make a login form using xml. I have the xml file where are some data , like an ID and a Password and I want to use them for a login. Here is my code

XmlDocument doc = new XmlDocument();
string filename = @"D:\Poriecte Visual\INFO2017\INFO2017\bin\Debug\XMLFile1.xml";
doc.Load(filename);

var Username = "";
var Password = "";

foreach (XmlNode node in doc.SelectNodes("Persoane"))
{
    Username = node.SelectSingleNode("ID").InnerText; (*)
    Password = node.SelectSingleNode("Password").InnerText;

    if (Username.Equals(textBox3.Text) && Password.Equals(textBox4.Text))
    {
        Form a = new Form4();
        a.Show();
        this.Hide();
    }
    else
    {
        MessageBox.Show("something is wrong");
    }

and at the line with (*) I get this error: Object reference not set to an instance of an object.

Thank you for any kind of help ^^ btw, im still a beginner in c# so don't judge me if the code is not good ^^

 <?xml version="1.0" encoding="utf-8"?>
 <Persoane>
   <Angajat>
     <Nume_Prenume>Horatiu Necula</Nume_Prenume>
     <ID>horatiu</ID>
     <Parola>123</Parola>
     <Nr_de_telefon>0723626741</Nr_de_telefon>
     <Adresa>Valenii de munte ,PH</Adresa>
   </Angajat>
 </Persoane>

Later EDIT : with more dates:

    <?xml version="1.0" encoding="utf-8"?>
    <Persoane>
      <Angajat>
        <Nume_Prenume>horatiu</Nume_Prenume>
        <ID>id1< /ID>
        <Password>123< /Password>
        <Nr_de_telefon>1</Nr_de_telefon>
        <Adresa>1</Adresa>
      </Angajat>
    <Angajat>
      <Nume_Prenume>a</Nume_Prenume>
      <ID>id2</ID>
      <Password>1234</Password>
      <Nr_de_telefon>1</Nr_de_telefon>
      <Adresa>1</Adresa>
    </Angajat>
   <Angajat>
    <Nume_Prenume>2</Nume_Prenume>
    <ID>id3</ID>
    <Password>12345</Password>
    <Nr_de_telefon>a</Nr_de_telefon>
    <Adresa>a</Adresa>
   </Angajat>
 </Persoane>
Peter
  • 3,916
  • 1
  • 22
  • 43

1 Answers1

1

The comments are correct. I revised the answer:

The "ID" node is not right underneath the "Persoane" node. You might want to look into XPath syntax: https://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx

In your case it needs to be

Username = node.SelectSingleNode(".//ID").InnerText;
Password = node.SelectSingleNode(".//Password").InnerText;
Peter
  • 3,916
  • 1
  • 22
  • 43
  • Note that "//ID" (all nodes with this name stating from the root) is completely wrong suggestion based on the goal to have list of users in XML. – Alexei Levenkov Sep 20 '16 at 17:56
  • Side note: please check out this META article about linking to w3schools -http://meta.stackoverflow.com/questions/280478/why-not-w3schools-com – Alexei Levenkov Sep 20 '16 at 17:58
  • @AlexeiLevenkov is right, it works only for my first id and password – Necula Horaţiu Sep 20 '16 at 18:00
  • Your comments are correct. I should have looked better into the problem set. I revised the answer – Peter Sep 20 '16 at 18:16
  • @Peter still doesn't work just with my first id/password I've tried something else but still same thing :\ – Necula Horaţiu Sep 20 '16 at 18:43
  • Are you sure it is the same issue? How does the xml for multiple users look like? You might have to change your code to `doc.SelectNodes("Angajat")` (depending on the actual xml format). – Peter Sep 20 '16 at 18:47
  • I edited my question and you have the xml file with more people @Peter – Necula Horaţiu Sep 20 '16 at 18:58
  • Ok. In this case you need to select the `Angajat` elements: `foreach (XmlNode p in doc.SelectNodes("//Angajat"))` – Peter Sep 20 '16 at 19:02
  • Now the problem is if i have 3 people and I enter the id and the password for one it open the form but also show the message that id or password is wrong @Peter – Necula Horaţiu Sep 20 '16 at 19:19
  • That is another design issue. Think about restructuring your code to check for valid users and passwords. – Peter Sep 20 '16 at 19:22