C#Xml文档员工管理系统(简单版)



Program.cs

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

namespace XMLDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("--------------------------员工管理系统----------------------");
                Console.WriteLine("欢迎进入员工管理系统:\n\t1.添加员工\n\t2.查询员工信息\n\t3.退出");
                Console.WriteLine("请输入您的选择:");
                string choiceNumber = Console.ReadLine();
                if (choiceNumber == "1")
                {
                    Console.WriteLine("请输入姓名:");
                    string name = Console.ReadLine();
                    Console.WriteLine("请输入年龄:");
                    string age = Console.ReadLine();
                    Console.WriteLine("请输入性别:");
                    string sex = Console.ReadLine();
                    User use1 = new User(name,age,sex);
                    XmlUser xm = new XmlUser();
                    xm.UserAdd(use1);
                    Console.WriteLine("增添成功!");
                }
                if (choiceNumber == "2")
                {
                    Console.WriteLine("员工信息如下:\n\t姓名\t年龄\t性别");
                    XmlUser xm = new XmlUser();
                    List list = new List();                  
                     list = xm.ShowUser();
                    foreach (User item in list)
                    {
                        Console.WriteLine("\t{0}\t{1}\t{2}", item.Name, item.Age,item.Sex);
                    }
                }
                if(choiceNumber == "3")
                {
                    break;
                }
            }           
              Console.ReadKey();
        }
    }
}


XmlUser类

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

namespace XMLDemo
{
    class XmlUser
    {
        /// 
        /// xml地址
        /// 
        public string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "user.xml";

        /// 
        /// 添加员工
        /// 
        /// 实例对象
        public void UserAdd(User user)
        {
            if (!File.Exists(xmlPath))
            {
                CreateXml(xmlPath, "Users");
            }
            //创建一个xml文档,加载到内存中去
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlPath);//写入缓冲区

            XmlNode node = doc.CreateNode(XmlNodeType.Element, "use", null);
            node.InnerXml = String.Format("{0}{1}{2}", user.Name, user.Age, user.Sex);
            doc.SelectSingleNode("/Users").AppendChild(node);
            doc.Save(xmlPath);//保存
        }

        public List ShowUser()
        {
            if (!File.Exists(xmlPath))
            {
                CreateXml(xmlPath, "Users");
            }
            //创建一个xml文档,加载到内存中去
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlPath);

            XmlNodeList xmlList = doc.SelectNodes("/Users/use");
            List listUser = new List();
            foreach (XmlNode item in xmlList)
            {
                string name = item.SelectSingleNode("Name").InnerText;
                string age = item.SelectSingleNode("Age").InnerText;
                string sex = item.SelectSingleNode("Sex").InnerText;
                User u = new User(name, age, sex);
                listUser.Add(u);
            }

            return listUser;
        }

        /// 
        /// 创建一个xml文档
        /// 
        public void CreateXml(string path, string baseContent)
        {
            //xml文档的初始化设置
            XmlWriterSettings setting = new XmlWriterSettings();
            setting.Encoding = Encoding.UTF8;
            setting.Indent = true;
            setting.IndentChars = "\t";
            //写入xml文档
            using (XmlWriter xw = XmlWriter.Create(path, setting))
            {
                xw.WriteStartElement(baseContent);//创建根目录
                xw.WriteEndElement();
                xw.Flush();//强制让缓冲区的文件流发出
            }


        }
    }
}

User类

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

namespace XMLDemo
{
    class User
    {
        public string Name { set; get; }//用户名
        public string Age { set; get; }//年龄
        public string Sex { set; get; }//性别

        public User(string name, string age, string sex)
        {
            this.Name = name;
            this.Age = age;
            this.Sex = sex;
        }
    }
}





你可能感兴趣的:(C#入门疑难知识点,xml员工管理系统,C#)