C#简单操作XML

类文件:

 class OperatorXML
    { 
        /// <summary>
        /// 确定资源文件路径,Resource为自己创建的目录
        /// </summary>
        private string filePath = @"....ResourcesXMLFile.xml";
        /// <summary>
        /// 
        /// </summary>
        private XDocument xDoc ;

        /// <summary>
        /// 当窗体加载时, 遍历所有节点
        /// </summary>
        public List<Desktop> LoadNode()
        {
            List<Desktop> desktop = new List<Desktop>();
            try
            {
                this.xDoc = XDocument.Load(filePath);//加载xml文件
                XElement root = xDoc.Root;  //获取根节点

                //遍历所有节点
                foreach (XElement elem in xDoc.Root.Elements("Desktop"))
                { 
                    desktop.Add(new Desktop(elem.Attribute("IpAddr").Value,elem.Attribute("Alias").Value, elem.Attribute("Pwd").Value,elem.Attribute("UserName").Value, elem.Attribute("Description").Value, new Guid(elem.Attribute("Id").Value)));
                }
                
            }
            catch(Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.ToString());
            }
            return desktop;
        }

        /// <summary>
        /// 遍历所有节点, 获取节点的别名
        /// </summary>
        /// <returns></returns>
        public List<string> GetNodeName()
        {
            List<string> nodeName = new List<string>();
            try
            {
                this.xDoc = XDocument.Load(filePath);//加载xml文件
                XElement root = xDoc.Root;  //获取根节点

                //遍历所有节点
                foreach (XElement elem in xDoc.Root.Elements("Desktop"))
                {
                    nodeName.Add(elem.Attribute("Alias").Value);
                }
                
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.ToString());
            }
            return nodeName;
        }


        /// <summary>
        /// 根据指定的id获取对应的节点属性
        /// </summary>
        /// <param name="id">指定的id</param>
        /// <returns></returns>
        public Desktop GetDesktop(string id)
        {
            this.xDoc = XDocument.Load(filePath);//加载xml文件
            XElement root = xDoc.Root;  //获取根节点
            Desktop desktop = null;
            //遍历所有节点
            foreach (XElement elem in xDoc.Root.Elements("Desktop"))
            {
                if (elem.Attribute("Id").Value == id)
                {
                    desktop = new Desktop();
                    desktop.IpAddr = elem.Attribute("IpAddr").Value;
                    desktop.Alias = elem.Attribute("Alias").Value;
                    desktop.UserName = elem.Attribute("UserName").Value;
                    desktop.Pwd = elem.Attribute("Pwd").Value;
                    desktop.Description = elem.Attribute("Description").Value;
                    desktop.Id = new Guid(elem.Attribute("Id").Value);
                }
            }
            return desktop;
        }

        /// <summary>
        /// 添加一个节点
        /// </summary>
        public  void AddSave(Desktop desktop)
        {
            try
            {
                this.xDoc = XDocument.Load(this.filePath);
                
                this.xDoc.Root.Add(new XElement("Desktop",
                                    new XAttribute("IpAddr",desktop.IpAddr),
                                     new XAttribute("UserName", desktop.UserName),
                                     new XAttribute("Description", desktop.Description),
                                     new XAttribute("Pwd",desktop.Pwd),
                                     new XAttribute("Alias", desktop.Alias),
                                     new XAttribute("Id", desktop.Id)
                                    ));
                this.xDoc.Save(this.filePath);
            }
            catch(Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.ToString());
            }
        }

        /// <summary>
        /// 根据ID来修改某个节点
        /// </summary>
        /// <param name="id"></param>
        public void modifyNode(Desktop desktop)
        {
            this.xDoc = XDocument.Load(this.filePath);
            foreach (XElement elem in xDoc.Root.Elements("Desktop"))
            {
                if (elem.Attribute("Id").Value == desktop.Id.ToString())
                {
                    elem.SetAttributeValue("IpAddr", desktop.IpAddr);
                    elem.SetAttributeValue("Alias", desktop.Alias);
                    elem.SetAttributeValue("UserName", desktop.UserName);
                    elem.SetAttributeValue("Pwd", desktop.Pwd);
                    elem.SetAttributeValue("Description", desktop.Description);
                }
            }
            this.xDoc.Save(this.filePath);
        }

        /// <summary>
        /// 删除一个节点并保存
        /// </summary>
        /// <param name="desktop"></param>
        public  bool  RemoveSave(string id)
        {
            bool isOK = false;
             this.xDoc = XDocument.Load(this.filePath);
             foreach (XElement elem in xDoc.Root.Elements("Desktop"))
             {
                 if (elem.Attribute("Id").Value == id) 
                 {
                     elem.Remove();
                     this.xDoc.Save(this.filePath);
                     isOK = true;
                     break;
                 }
             }
            return isOK;
        }
    }

xml文件示例(XMLFile.xml):

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Desktop IpAddr="host1" UserName="administrator" Description="独立服务器" Pwd="pwd" Alias="礼仪" Id="dfbc7b37-b5b3-4c36-a9c4-64302d824c34" />
  <Desktop IpAddr="host2" UserName="administrator" Description="咖啡独立服务器" Pwd="pwsd" Alias="季卡" Id="80da53e6-5d84-4dc3-bb3f-90fbf0174e06" />
</Root>
原文地址:https://www.cnblogs.com/wxylog/p/7216772.html