xml 相关操作

    注释写的足够清楚 。嗯 ,以后注释一定要写好 ,最主要是的不知道什么时候会用 ,完全可以第一时间内就看得懂 。

       

        /// <summary>
        /// 向xml文件添加节点信息
        /// </summary>
        /// <param name="xmlPath"></param>
        public static bool AddXmlNodeInformation(string xmlPath, string link, string title)
        {
            try
            {
                //定义并从xml文件中加载节点(根节点)
                XElement root = XElement.Load(xmlPath);

                //定义并从xml文件中加载节点(根节点)
                IEnumerable<XElement> targetNodes = from target in XElement.Load(xmlPath).Descendants("menu")
                                                    select target;
                int count = 0;
                foreach (XElement node in targetNodes)
                {
                    if (node.HasElements)
                    {
                        count = Convert.ToInt32(node.LastNode.ToString().Substring(7, 1));
                    }
                    else
                    {
                        count = 0;
                    }
                }
                //定义一个新节点
                XElement newNode = new XElement("m", new XAttribute("id", count + 1),
                                                                new XElement("link", link),
                                                                new XElement("title", title)
                                                                );
                //将此新节点添加到根节点下
                root.Element("menu").Add(newNode);
                //保存对xml的更改操作
                root.Save(xmlPath);
                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw new Exception("向xml文件添加节点信息失败!" + ex.Message);
            }
        }

        /// <summary>
        ///  遍历xml文件的节点信息
        /// </summary>
        /// <param name="xmlPath"></param>
        public static List<string> GetXmlNodeInformation(string xmlPath)
        {
            List<string> list = new List<string>();
            string menu = string.Empty;
            try
            {
                //定义并从xml文件中加载节点(根节点)
                XElement rootNode = XElement.Load(xmlPath);

                //查询语句: 获得根节点下menu子节点(此时的子节点可以跨层次:孙节点、重孙节点......)
                IEnumerable<XElement> targetNodes = from target in rootNode.Descendants("menu").Descendants("m")
                                                    select target;
                foreach (XElement node in targetNodes)
                {

                    string id = node.Attribute("id").Value;
                    string title = node.Element("title").Value;
                    string link = node.Element("link").Value;
                    menu = string.Concat(id, "$", title, "$", link);
                    list.Add(menu);
                }
                return list;
            }
            catch (Exception ex)
            {
                throw new Exception("查询menu结点信息失败!" + ex.Message);
            }
        }

        /// <summary>
        /// 查询attribute属性
        /// </summary>
        /// <param name="xmlPath"></param>
        /// <returns></returns>
        public static string GetAttribute(string xmlPath)
        {
            try
            {
                string menu = string.Empty;
                //定义并从xml文件中加载节点(根节点)
                XElement rootNode = XElement.Load(xmlPath);

                //查询语句: 获得根节点下menu子节点(此时的子节点可以跨层次:孙节点、重孙节点......)
                IEnumerable<XElement> targetNodes = from target in rootNode.Descendants("menu")
                                                    select target;
                foreach (XElement node in targetNodes)
                {
                    menu = node.Attribute("value").Value;
                }
                return menu;
            }
            catch (Exception ex)
            {
                throw new Exception("查询attribute属性失败!" + ex.Message);
            }
        }

        /// <summary>
        /// 修改attribute属性
        /// </summary>
        /// <param name="xmlPath"></param>
        /// <param name="attribute"></param>
        /// <returns></returns>
        public static bool ModifyAttribute(string xmlPath, string attribute)
        {
            try
            {
                //定义并从xml文件中加载节点(根节点)
                XElement rootNode = XElement.Load(xmlPath);

                //将获得的节点集合中的每一个节点依次从它相应的父节点中删除
                rootNode.Element("menu").Attribute("value").SetValue(attribute);
                rootNode.Save(xmlPath);
                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw new Exception("修改attribute属性失败!" + ex.Message);
            }
        }

        /// <summary>
        /// 修改xml文件的节点信息
        /// </summary>
        /// <param name="xmlPath"></param>
        public static bool ModifyXmlNodeInformation(string xmlPath, string id, string link, string title)
        {
            try
            {
                //定义并从xml文件中加载节点(根节点)
                XElement rootNode = XElement.Load(xmlPath);
                //查询语句: 获取ID属性值等于id的所有User节点(或条件用"||"符号连接)

                IEnumerable<XElement> targetNodes = from target in rootNode.Descendants("menu")
                                                    where target.Attribute("ID").Value == id
                                                    select target;
                //遍历所获得的目标节点(集合)
                foreach (XElement node in targetNodes)
                {
                    //将 节点的InnerText设置
                    node.Element("link").SetValue(link);
                    node.Element("title").SetValue(title);
                }
                //保存对xml的更改操作
                rootNode.Save(xmlPath);
                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw new Exception("修改xml文件的节点信息失败!" + ex.Message);
            }
        }

        /// <summary>
        /// 修改xml文件的节点信息
        /// </summary>
        /// <param name="xmlPath"></param>
        public static bool ModifyXmlNodeInformation(string xmlPath, List<string> list)
        {
            try
            {
                //定义并从xml文件中加载节点(根节点)
                XElement rootNode = XElement.Load(xmlPath);

                //将获得的节点集合中的每一个节点依次从它相应的父节点中删除
                rootNode.Element("menu").Nodes().Remove();
                XElement newNode = null;
                for (int i = 0; i < list.Count; i++)
                {
                    string[] strarr = list[i].Split('$');
                    newNode = new XElement("m", new XAttribute("id", i),
                                                              new XElement("link", strarr[1]),
                                                              new XElement("title", strarr[0])

                  );
                    //将此新节点添加到根节点下
                    rootNode.Element("menu").Add(newNode);
                }

                //保存对xml的更改操作
                rootNode.Save(xmlPath);
                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw new Exception("修改xml文件的节点信息失败!" + ex.Message);
            }
        }

        /// <summary>
        /// 删除指定xml文件的节点信息
        /// </summary>
        /// <param name="xmlPath"></param>
        public static bool DeleteXmlNodeInformation(string xmlPath, string id)
        {
            try
            {
                //定义并从xml文件中加载节点(根节点)
                XElement rootNode = XElement.Load(xmlPath);
                //查询语句: 获取ID属性值等于"XXXX"的所有User节点
                IEnumerable<XElement> nodes = rootNode.Element("menu").Elements();
                foreach (var item in nodes)
                {
                    if (item.Attribute("id").Value == id)
                    {
                        item.Remove();
                        break;
                    }
                }
                //保存对xml的更改操作
                rootNode.Save(xmlPath);
                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw new Exception("删除指定xml文件的节点信息失败!" + ex.Message);
            }
        }

  

原文地址:https://www.cnblogs.com/lelese7en/p/2707088.html