zoukankan      html  css  js  c++  java
  • 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);
                    }
                }
            }
        }
    }

     

    天生我材必有用,千金散尽还复来
  • 相关阅读:
    Hibernate配置
    Log4j 局部笔记
    有关接口 笔记 懒人版
    JAVA面向对象编程这本书的摘录~!(2016-5-23)
    关于关闭数据流
    安卓桌面开发小应用
    ACM Sdut 2158 Hello World!(数学题,排序) (山东省ACM第一届省赛C题)
    hdu 1573 A/B (扩展欧几里得)
    hdu 1788 Chinese remainder theorem again(最小公倍数)
    ACM hdu 1019 Least Common Multiple
  • 原文地址:https://www.cnblogs.com/ligenyun/p/9072617.html
Copyright © 2011-2022 走看看