• c# 操作XML


    实例:增删改查

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;
    
    namespace ConsoleApp1
    {
        public class XMLHelper
        {
            public void Add()
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("XMLFile1.xml");
    
                var root = doc.DocumentElement;
    
                var newnode = doc.CreateNode("element", "student", "");
              
    
                var newnode1 = doc.CreateNode("element", "sno", "");
                newnode1.InnerText = "4";
                var newnode2 = doc.CreateNode("element", "sname", "");
                newnode2.InnerText = "成龙";
    
                newnode.AppendChild(newnode1);
                newnode.AppendChild(newnode2);
    
    
                root.AppendChild(newnode);
    
    
                doc.Save("XMLFile1.xml");            
            }
    
            public void Delete()
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("XMLFile1.xml");
    
                var root = doc.DocumentElement;
    
                root.RemoveChild(root.FirstChild);
                
                doc.Save("XMLFile1.xml");
            }
    
            public void Update()
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("XMLFile1.xml");
    
                var root = doc.DocumentElement;
    
                var first = root.FirstChild.FirstChild;
                first.InnerText = "fffffffffff";
    
                doc.Save("XMLFile1.xml");
            }
    
            public void GetNodes()
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("XMLFile1.xml");
    
                var root = doc.DocumentElement;
                var find = root.ChildNodes[1].InnerText;
                Console.WriteLine(find);
            }
    
            public void GetOneNodeByName()
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("XMLFile1.xml");
    
                var root = doc.DocumentElement;
    
                foreach (XmlElement item in root.ChildNodes)
                {
                    if(item.FirstChild.InnerText == "4")
                    {
                        Console.WriteLine(item.OuterXml);
                    }
                }
            }
        }
    }

     

    天生我材必有用,千金散尽还复来
  • 相关阅读:
    通过具名 slot (插槽)来显示Dialog 的标题
    elementUI 中,table表格如何实现当某一行被点击时会触发该事件(row-click)
    switch循环
    CSS动画
    for循环
    Display
    修改页面标题前的图标
    from表单
    CSS3文字效果
    CSS颜色渐变
  • 原文地址:https://www.cnblogs.com/ligenyun/p/9072617.html
Copyright © 2020-2023  润新知