C# XML 写入

写入内容为如图

 写入xml文件方法

  public void CreatXmlTree(string xmlPath)
        {
            XElement xElement = new XElement(
                new XElement("BookStore",
                    new XElement("Book",
                        new XElement("Name", "C#入门", new XAttribute("BookName", "C#")),
                        new XElement("Author", "Martin", new XAttribute("Name", "Martin")),
                        new XElement("Adress", "上海"),
                        new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd"))
                     ),

                    new XElement("Book",
                        new XElement("Name", "WCF入门", new XAttribute("BookName", "WCF")),
                        new XElement("Author", "Mary", new XAttribute("Name", "Mary")),
                        new XElement("Adress", "北京"),
                        new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd"))
                        )));




            //需要指定编码格式,否则在读取时会抛:根级别上的数据无效。 第 1 行 位置 1异常
            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();
        }
View Code

调用写入方法

     private void simpleButton4_Click(object sender, EventArgs e)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory;
            XmlDocument xmlDoc = new XmlDocument();
            string path1 = @path + "configxml.xml";
            CreatXmlTree(path1);
        }
View Code

新增节点

   private void simpleButton5_Click(object sender, EventArgs e)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory;
            XmlDocument xmlDoc = new XmlDocument();
            string path1 = @path + "conxml.xml";
            Create(path1);
        }
       //新增节点NewBook并增加属性Name="WPF"
        public void Create(string xmlPath)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlPath);
            
            var root = xmlDoc.DocumentElement;//取到根结点
            XmlNode newNode = xmlDoc.CreateNode("element", "NewBook", "");
            newNode.InnerText = "WPF";

            //添加为根元素的第一层子结点
            root.AppendChild(newNode);
            xmlDoc.Save(xmlPath);
        }
View Code

新增效果如图

修改类型:操作xml节点属性主要用XmlElement对象所以取到结点后要转类型 

 private void simpleButton6_Click(object sender, EventArgs e)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory;
            XmlDocument xmlDoc = new XmlDocument();
            string path1 = @path + "conxml.xml";
            CreateAttribute(path1);
        }
        public void CreateAttribute(string xmlPath)
       {
           XmlDocument xmlDoc = new XmlDocument();
           xmlDoc.Load(xmlPath);
           var root = xmlDoc.DocumentElement;//取到根结点
           XmlElement node = (XmlElement)xmlDoc.SelectSingleNode("BookStore/NewBook");
           node.SetAttribute("Name", "WPF");
           xmlDoc.Save(xmlPath);

      
        }
View Code

效果如图

删除节点与属性

  //删除节点
       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);
       }
View Code

调用删除节点的方法

  private void simpleButton6_Click(object sender, EventArgs e)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory;
            XmlDocument xmlDoc = new XmlDocument();
            string path1 = @path + "conxml.xml";
            //CreateAttribute(path1);
            Delete(path1);
        }
View Code

 删除属性

     //删除属性
        public void DeleteAttribute(string xmlPath)
      {
          XmlDocument xmlDoc = new XmlDocument();
          xmlDoc.Load(xmlPath);
          XmlElement node = (XmlElement)xmlDoc.SelectSingleNode("BookStore/NewBook");
          //移除指定属性
          node.RemoveAttribute("Name");
          //移除当前节点所有属性,不包括默认属性
          //node.RemoveAllAttributes();
        xmlDoc.Save(xmlPath);
    }

 del删除node

#region node方法可行

            //if (parentId == 0)
            //{
            //    MessageBox.Show("一级节点不能删除");
            //}
            //string path = System.AppDomain.CurrentDomain.BaseDirectory;
            //XmlDocument xmlDoc = new XmlDocument();
            //string path1 = @path + "DeviceConfig\\DeviceConfigFile.xml";
            //xmlDoc.Load(path1);
            //XmlNode xnRoot = xmlDoc.SelectSingleNode("DevicesConfig");//根节点
            //foreach (XmlNode node in xnRoot.ChildNodes)
            //{
            //    string root2Name = node.Name.Replace("ListConfig", "");//1级节点
            //    if (root2Name == parentNodeName)
            //    {
            //        foreach (XmlNode item in node)
            //        {
            //            var txt = item.SelectSingleNode("DeviceTypeConfig");
            //            XmlNode node3 = item.SelectSingleNode("DeviceTypeConfig");
            //            string root3Name = txt.InnerText;
            //            if (root3Name == SDNodeText)
            //            {
            //                node.RemoveChild(item);//删除指定的子节点
            //                xmlDoc.Save(path1);
            //                break;
            //            }
            //        }
            //    }
            //}
            //ShowTree2();
C#.net. WPF.core 技术交流群 群号205082182,欢迎加入,也可以直接点击左侧和下方的"加入QQ群",直接加入
原文地址:https://www.cnblogs.com/aijiao/p/15554756.html