C# Login XML 循环错误密码
C# Login XML looping wrong password
我在使用 C# 和从 XML 文件加载时遇到小问题。 (只是一个通知 - 学生项目)
我正在使用 XML 制作登录表单,如果 XML 文件中只有一个帐户,这很好用,但如果有更多帐户,如果我 select除了第一个程序之外的任何程序都会加载一个帐户,然后循环 X 次并说 "Wrong username or password"
public static bool Login(bool UserExists)
{
string sUsername;
string sPassword;
string check;
int nPick;
Console.WriteLine("Insert username: ");
sUsername = Console.ReadLine();
Console.WriteLine("Insert password: ");
sPassword = Console.ReadLine();
List<User> lUser = GetUsers();
for (int i = 0; i < lUser.Count(); i++)
{
if (lUser[i].username == sUsername && lUser[i].password == sPassword)
{
UserExists = true;
Console.WriteLine("\n Login successfull!");
do
{
Menu();
check = Console.ReadLine();
int.TryParse(check, out nPick);
switch (nPick)
{
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
System.Environment.Exit(0);
break;
default:
Console.WriteLine("\n Option not existing!");
break;
}
}
while (nPick != 5);
}
else
{
Console.WriteLine("\n Wrong username or password!\n");
}
}
return UserExists;
}
public static List<User> GetUsers() {
List<User> lUser = new List<User>();
string sXml = "";
StreamReader oSr = new StreamReader("X");
using (oSr)
{
sXml = oSr.ReadToEnd();
}
XmlDocument oXml = new XmlDocument();
oXml.LoadXml(sXml);
XmlNodeList oNodes = oXml.SelectNodes("//data/user");
foreach (XmlNode oNode in oNodes)
{
string u = oNode["username"].InnerText;
string p = oNode["password"].InnerText;
User oUser = new User(u, p); lUser.Add(oUser);
}
return lUser;
}
编辑 1:添加了 XML
<data>
<user>
<username>admin</username>
<password>admin</password>
</user>
<user>
<username>user</username>
<password>user</password>
</user>
<user>
<username>user1</username>
<password>user1</password>
</user>
<user>
<username>user2</username>
<password>user2</password>
</user>
</data>
例如
1) 如果我输入 "admin" "admin" 组合,程序将进入菜单
2) 如果我输入"user1" "user1" 会登录,还要写3次"Wrong username/password"
你离答案很近了。您可能希望遍历每个用户,并且仅在 none 匹配的情况下才打印错误消息。
请注意,我还将 UserExists
布尔值移到了函数变量中,而不是将其作为参数。
public static bool Login()
{
string sUsername;
string sPassword;
string check;
int nPick;
bool UserExists = false;
Console.WriteLine("Insert username: ");
sUsername = Console.ReadLine();
Console.WriteLine("Insert password: ");
sPassword = Console.ReadLine();
List<User> lUser = GetUsers();
for (int i = 0; i < lUser.Count(); i++)
{
if (lUser[i].username == sUsername && lUser[i].password == sPassword)
{
UserExists = true;
Console.WriteLine("\n Login successfull!");
do
{
Menu();
check = Console.ReadLine();
int.TryParse(check, out nPick);
switch (nPick)
{
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
System.Environment.Exit(0);
break;
default:
Console.WriteLine("\n Option not existing!");
break;
}
}
while (nPick != 5);
}
}
if (!UserExists) {
Console.WriteLine("\n Wrong username or password!\n");
}
return UserExists;
}
我在使用 C# 和从 XML 文件加载时遇到小问题。 (只是一个通知 - 学生项目)
我正在使用 XML 制作登录表单,如果 XML 文件中只有一个帐户,这很好用,但如果有更多帐户,如果我 select除了第一个程序之外的任何程序都会加载一个帐户,然后循环 X 次并说 "Wrong username or password"
public static bool Login(bool UserExists)
{
string sUsername;
string sPassword;
string check;
int nPick;
Console.WriteLine("Insert username: ");
sUsername = Console.ReadLine();
Console.WriteLine("Insert password: ");
sPassword = Console.ReadLine();
List<User> lUser = GetUsers();
for (int i = 0; i < lUser.Count(); i++)
{
if (lUser[i].username == sUsername && lUser[i].password == sPassword)
{
UserExists = true;
Console.WriteLine("\n Login successfull!");
do
{
Menu();
check = Console.ReadLine();
int.TryParse(check, out nPick);
switch (nPick)
{
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
System.Environment.Exit(0);
break;
default:
Console.WriteLine("\n Option not existing!");
break;
}
}
while (nPick != 5);
}
else
{
Console.WriteLine("\n Wrong username or password!\n");
}
}
return UserExists;
}
public static List<User> GetUsers() {
List<User> lUser = new List<User>();
string sXml = "";
StreamReader oSr = new StreamReader("X");
using (oSr)
{
sXml = oSr.ReadToEnd();
}
XmlDocument oXml = new XmlDocument();
oXml.LoadXml(sXml);
XmlNodeList oNodes = oXml.SelectNodes("//data/user");
foreach (XmlNode oNode in oNodes)
{
string u = oNode["username"].InnerText;
string p = oNode["password"].InnerText;
User oUser = new User(u, p); lUser.Add(oUser);
}
return lUser;
}
编辑 1:添加了 XML
<data>
<user>
<username>admin</username>
<password>admin</password>
</user>
<user>
<username>user</username>
<password>user</password>
</user>
<user>
<username>user1</username>
<password>user1</password>
</user>
<user>
<username>user2</username>
<password>user2</password>
</user>
</data>
例如
1) 如果我输入 "admin" "admin" 组合,程序将进入菜单
2) 如果我输入"user1" "user1" 会登录,还要写3次"Wrong username/password"
你离答案很近了。您可能希望遍历每个用户,并且仅在 none 匹配的情况下才打印错误消息。
请注意,我还将 UserExists
布尔值移到了函数变量中,而不是将其作为参数。
public static bool Login()
{
string sUsername;
string sPassword;
string check;
int nPick;
bool UserExists = false;
Console.WriteLine("Insert username: ");
sUsername = Console.ReadLine();
Console.WriteLine("Insert password: ");
sPassword = Console.ReadLine();
List<User> lUser = GetUsers();
for (int i = 0; i < lUser.Count(); i++)
{
if (lUser[i].username == sUsername && lUser[i].password == sPassword)
{
UserExists = true;
Console.WriteLine("\n Login successfull!");
do
{
Menu();
check = Console.ReadLine();
int.TryParse(check, out nPick);
switch (nPick)
{
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
System.Environment.Exit(0);
break;
default:
Console.WriteLine("\n Option not existing!");
break;
}
}
while (nPick != 5);
}
}
if (!UserExists) {
Console.WriteLine("\n Wrong username or password!\n");
}
return UserExists;
}