C#判断登录是否成功

准备界面元素

C#判断登录是否成功_第1张图片
登录界面.jpg

准备XML数据



 
   Lucy
   18
   
 
 
   Luck
   19
   
 
 
   Jack
   20
   
 
 
   Tom
   21
   
 
 
   Marry
   22
   
 
 
   Bill
   23
   
 

根据数据准备类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LoginExample
{
    class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public Char Gender { get; set; }

        //重写Equals方法
        public override bool Equals(object obj)
        {
            Student s = obj as Student;
            if (this.ID==s.ID&&this.Name==s.Name&&this.Gender==s.Gender&&this.Age==s.Age)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

用户输入数据与XML数据比较

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace LoginExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //Student对象集合
        List list = new List();
        //窗口加载
        private void Form1_Load(object sender, EventArgs e)
        {
           //创建XML对象
            XmlDocument xdoc = new XmlDocument();
            //载入XML数据
            xdoc.Load("Student.xml");
            //获取XML根节点
            XmlElement Person = xdoc.DocumentElement;
            //获取根节点下的子节点
            XmlNodeList xnl = Person.ChildNodes;
            //将子节点数据存入Student对象后放入List集合
            foreach (XmlNode item in xnl)
            {
                int id = Convert.ToInt32(item.Attributes["StudentID"].Value);
                String name = item["Name"].InnerText;
                int age = Convert.ToInt32(item["Age"].InnerText);
                Char gender = Convert.ToChar(item["Gender"].InnerText);

                Student Stu = new Student();
                Stu.ID = id;
                Stu.Age = age;
                Stu.Name = name;
                Stu.Gender = gender;
                list.Add(Stu);
            }
            Console.WriteLine();
        }

        private void LoginButton_Click(object sender, EventArgs e)
        {
            //获取登录界面数据
            string name = TexName.Text.Trim();
            int age = Convert.ToInt32(TexAge.Text.Trim());
            int id = Convert.ToInt32(TexID.Text.Trim());
            char gender = RbManButton.Checked ? '男' : '女';
            //将登录对象与List集合中的对象进行比较
            Student LoginStudent = new Student();
            LoginStudent.ID = id;
            LoginStudent.Name = name;
            LoginStudent.Age = age;
            LoginStudent.Gender = gender;
            bool isLogin = false;
            foreach (var item in list)
            {
                if (item.Equals(LoginStudent))
                {
                    MessageBox.Show("登录成功!");
                    isLogin = true;
                }
            }
            if (isLogin==false)
            {
                MessageBox.Show("登录失败");
            }
        }
    }
}

运行界面

C#判断登录是否成功_第2张图片
运行效果.jpg

你可能感兴趣的:(C#判断登录是否成功)