c#环境XML文件创建、修改、删除、查找相关操作

本人在开发Windows应用中偶然碰到了需要将应用数据存储到本地问题,如果用SQLite数据库操作起来有一定的问题,于是将想到了将数据存储到本地XML文件中,如果有问题,欢迎各位大佬指点。

我碰到的问题是保存用户信息,每一个用户有Id,name,age,phone,symptom五个属性。
1、首先创建一个结构体如下

        public struct UserMessage
        {
            public string Id;
            public string Name;
            public string Age;
            public string Phone;
            public string Symptom;

            public UserMessage(string Id, string Name, string Age, string Phone, string Symptom)
            {
                this.Id = Id;
                this.Name = Name;
                this.Age = Age;
                this.Phone = Phone;
                this.Symptom = Symptom;
            }
        }

这个基本每个人都会,不多说。

2、创建xml文档
创建xml文档需要指定文档存储的地址,并且添加相应的节点。

        public void CreatXmlTree(string xmlPath)
        {
            XElement xElement = new XElement(
                 new XElement("UserList",
                    new XElement("User",
                        new XAttribute("Id", "000"),
                        new XAttribute("Name", "000"),
                        new XAttribute("Age", "000"),
                        new XAttribute("Phone", "000"),
                        new XAttribute("Symptom", "000")
                        )
                        )
                );

            //需要指定编码格式,否则在读取时会抛:根级别上的数据无效。
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = new UTF8Encoding(false);
            settings.Indent = true;
            XmlWriter xw = XmlWriter.Create(xmlPath, settings);
            xElement.Save(xw);
            //写入文件
            xw.Flush();
            xw.Close();
        }

3、添加节点
类似于数据库,添加相同节点类似于数据库表中添加一条数据。

        public void AddNewNode(string xmlPath, UserMessage userMessage)
        {
            string xmlpath = xmlPath;

            UserMessage usermessage = userMessage;
            string id = usermessage.Id;
            string name = usermessage.Name;
            string age = usermessage.Age;
            string phone = usermessage.Phone;
            string symptom = usermessage.Symptom;

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlpath);
            XmlNode root = xmlDoc.SelectSingleNode("UserList");//查找<UserList>   
            XmlElement xe1 = xmlDoc.CreateElement("User");//创建一个<User>节点   
            xe1.SetAttribute("Id", id);//设置该节点id属性   
            xe1.SetAttribute("Name", name);//设置该节点name属性               
            xe1.SetAttribute("Age", age);//设置该节点age属性   
            xe1.SetAttribute("Phone", phone);//设置该节点phone属性   
            xe1.SetAttribute("Symptom", symptom);//设置该节点symptom属性   

            root.AppendChild(xe1);//添加到节点中   
            xmlDoc.Save(xmlpath);
        }

4、删除节点

        public void Delete(string xmlPath)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlPath);
            var root = xmlDoc.DocumentElement;//取到根结点

            var element = xmlDoc.SelectSingleNode("BookStore/NewBook");
            root.RemoveChild(element);
            xmlDoc.Save(xmlPath);
        }

5、查找数据
我们需要在xml文件中找出具体某个节点下的所有子节点以及他们的value值,并以结构体的形式返回。

        public UserMessage Search(string xmlPath, string Id)
        {
            string xmlpath = xmlPath;
            string id = Id;
            UserMessage usermessage = new UserMessage("", "", "", "", "");
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlPath); // 找到要添加的xml文件 
            XmlNode rootNode = xmlDoc.SelectSingleNode("UserList");//查找
            foreach (XmlNode xnf in rootNode.ChildNodes)
            {
                XmlElement xe = (XmlElement)xnf;
                if (xe.GetAttribute("Id").Equals(id))
                {
                    usermessage.Id = xe.GetAttribute("Id");  //得到属性值
                    usermessage.Name = xe.GetAttribute("Name");
                    usermessage.Age = xe.GetAttribute("Age");
                    usermessage.Phone = xe.GetAttribute("Phone");
                    usermessage.Symptom = xe.GetAttribute("Symptom");
                }
            }
            return usermessage;
        }
以上就是我用到的一下基本操作,如果哪里有不对的地方,欢迎批评指正。

猜你喜欢

转载自blog.csdn.net/cugzhaolc/article/details/78469187