using System.Xml;
namespace XML读取
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string userName = "";
string pwd = "";
bool valid = false;
private void label1_Click(object sender, EventArgs e)
{
XmlDocument myXml = new XmlDocument();
myXml.Load("user.xml");
XmlNode users = myXml.DocumentElement;
foreach (XmlNode node in users.ChildNodes)
{
if (node.Name == "user")
{
foreach (XmlNode user in node.ChildNodes)
{
switch (user.Name)
{
case "userName":
userName = user.InnerText;
break;
case "userPasw":
pwd = user.InnerText;
break;
}
if ((userName == txtName.Text.Trim()) && (pwd == txtPwd.Text.Trim()))
{
valid = true;
}
}
}
}
if (valid == true)
{
MessageBox.Show("登录成功!");
}
else
{
MessageBox.Show("登录失败!");
}
}
}
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<users>
<user id="1">
<userName>cxf</userName>
<userPasw>001</userPasw>
</user>
<user id="2">
<userName>1233</userName>
<userPasw>123</userPasw>
</user>
</users>