随笔 - 148  文章 - 1  评论 - 15  阅读 - 30万

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();
复制代码

 

posted on   冰魂雪魄  阅读(469)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2018-11-15 C# winform中 窗体缩放自适应的方法(不同电脑/不同分辨率)
2018-11-15 C# WinForm窗体及其控件自适应各种屏幕分辨率
2018-11-15 Socket网络编程
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

WPF框架交流群:C#.net. WPF.core 技术交流�      C#WPF技术交流群:C#.net. WPF.core 技术交流�     WPF技术大牛交流群:C#.net. WPF.core 技术交流�
点击右上角即可分享
微信分享提示