linq to xml之增改删查

 

有如下xml文档

<?xml version="1.0" encoding="utf-8"?>
<users>
  <user>
    <id>001</id>
    <name>xy</name>
    <sex>male</sex>
    <age>30</age>
    <university>清华</university>
    <phone>13328981122</phone>
  </user>
  <user>
    <id>002</id>
    <name>小明</name>
    <sex>male</sex>
    <age>19</age>
    <university>北大</university>
    <phone>13328980192</phone>
  </user>
  <user>
    <id>003</id>
    <name>小张</name>
    <sex>female</sex>
    <age>30</age>
    <university>南大</university>
    <phone>13328981152</phone>
  </user>
</users>   

对其进行操作

 

class XMLService : IXMLService
    {
        // xml文档路径
        string path = Environment.CurrentDirectory + "
\\xml\\communication.xml";
       
        XDocument doc = null;

        public void CreateXML()
        {         
            // 创建xml元素
            XElement root = new XElement("users",
                new XElement("user",
                    new XElement("id", "001"),
                    new XElement("name", "xy"),
                    new XElement("sex", "male"),
                    new XElement("age", "30"),
                    new XElement("university", "清华"),
                    new XElement("phone", "13328981122")
                    ),
                new XElement("user",
                    new XElement("id", "002"),
                    new XElement("name", "小明"),
                    new XElement("sex", "male"),
                    new XElement("age", "19"),
                    new XElement("university", "北大"),
                    new XElement("phone", "13328980192")
                    ),
                new XElement("user",
                    new XElement("id", "003"),
                    new XElement("name", "小张"),
                    new XElement("sex", "female"),
                    new XElement("age", "30"),
                    new XElement("university", "南大"),
                    new XElement("phone", "13328981152")
                    )
               );
            root.Save(path);
        }


        // 读出所有通讯录
        public IEnumerable<Information> ReadAllInformation()
        {
            var users = from c in XElement.Load(path).Elements("user")
                        select c;
            return ToInforList(users);
        }

        // 根据姓名查找
        public IEnumerable<Information> FindInfoByName(string name)
        {
            var users = from c in XElement.Load(path).Elements("user")
                        where c.Element("name").Value.Contains(name.Trim())
                        select c;

            return ToInforList(users);
        }

        // 转化成List<Information>
        public IEnumerable<Information> ToInforList(IEnumerable<XElement> users)
        {
            List<Information> listInfo = new List<Information>();
            foreach (var user in users)
            {
                Information info = new Information();
                info.Id = user.Element("id").Value.ToString();
                info.Name = user.Element("name").Value.ToString();
                info.Gender = user.Element("sex").Value.ToString();
                int age;
                if (int.TryParse(user.Element("age").Value.ToString(), out age))
                {
                    info.Age = age;
                }
                info.University = user.Element("university").Value.ToString();
                info.Phone = user.Element("phone").Value.ToString();
                listInfo.Add(info);
            }
            return listInfo;
        }

        // 删除
        public void DeleteByID(string id)
        {
            doc = XDocument.Load(path);
            doc.Element("users").Elements("user").

                   Where(ss => ss.Element("id").Value == id).Remove();
            doc.Save(path);
        }

        // 增加一条记录
        public void Add(string id, string name, string sex, int age, string school, string phone)
        {
            doc = XDocument.Load(path);
            doc.Element("users").Add(new XElement("user",
                                        new XElement("id", id),
                                        new XElement("name", name),
                                        new XElement("sex", sex),
                                        new XElement("age", age.ToString()),
                                        new XElement("university", school),
                                        new XElement("phone", phone)));
            doc.Save(path);

        }

 

        // 修改
        public void Modify

                (string id,string name, string sex, int age, string school, string phone)
        {
            doc = XDocument.Load(path);
            var user = (from u in doc.Element("users").Elements("user")
                        where u.Element("id").Value == id
                       select u).FirstOrDefault();
            user.Element("name").Value = name;
            user.Element("sex").Value = sex;
            user.Element("age").Value = age.ToString();
            user.Element("university").Value = school;
            user.Element("phone").Value = phone;
            doc.Save(path);
        }

上一篇:Solidity变量位置与GAS成本关系


下一篇:Java中的位运算