首页 > 解决方案 > 使用 xml 进行用户身份验证。C#

问题描述

我试图用这段代码循环它,但我只得到第一个用户名和密码正确,其他任何一个都不正确。这意味着它只是循环思考一个用户。

            XmlDocument mac = new XmlDocument();
            mac.Load(@"C:\Users\user\source\repos\lilis shop\lilis shop\Properties\XMLFile1.xml");
            XmlNodeList xnlist = mac.DocumentElement.SelectNodes("//logins//users");

            foreach (XmlNode naa in xnlist)
            {
                string username = naa["/username"].InnerText;
                string password = naa["/password"].InnerText;

                if (username == txtusername.Text && password == txtpassword.Text)
                {
                    Letsee = "LOGEDIN";
                    Form1 openform = new Form1();
                    openform.lablogin.Text = Letsee;
                    openform.lab1.Text = this.txtusername.Text;
                    openform.labelsingout.Text = "SINGOUT";

                    openform.Show();

                    this.Hide();
                    break;

                }

                else
                {
                    MessageBox.Show("please enter the correct password or username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtpassword.Text = null;
                    txtusername.Text = null;
                    break;
                }
            }

标签: c#xmlauthentication

解决方案


这可能是空格问题。您必须调试此 foreach 循环以检查确切值。


推荐阅读