zoukankan      html  css  js  c++  java
  • C#操作XML增删改查

    XML文件是一种常用的文件格式,不管是B/S还是C/S都随处可见XML的身影。Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具。XML是一种简单的数据存储语言,使用一系列简单的标记描述数据,而这些标记可以用方便的方式建立,虽然XML占用的空间比二进制数据要占用更多的空间,但XML极其简单易于掌握和使用。微软也提供了一系列类库来倒帮助我们在应用程序中存储XML文件。

        “在程序中访问进而操作XML文件一般有两种模型,分别是使用DOM(文档对象模型)和流模型,使用DOM的好处在于它允许编辑和更新XML文档,可以随机访问文档中的数据,可以使用XPath查询,但是,DOM的缺点在于它需要一次性的加载整个文档到内存中,对于大型的文档,这会造成资源问题。流模型很好的解决了这个问题,因为它对XML文件的访问采用的是流的概念,也就是说,任何时候在内存中只有当前节点,但它也有它的不足,它是只读的,仅向前的,不能在文档中执行向后导航操作。”

    XML文件创建

    首先来看一个简单的XML文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <Persons>
      <Person id="1">
        <Name>FlyElephant</Name>
        <Age>24</Age>
      </Person>
      <Person id="2">
        <Name>keso</Name>
        <Age>25</Age>
      </Person>
    </Persons>

     这是最常见的Dom形式的XML文件,创建的话也比较简单,代码如下:

         XmlDocument doc = new XmlDocument();
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                doc.AppendChild(dec);
                //根节点  
                XmlElement root = doc.CreateElement("Persons");
                doc.AppendChild(root);
    
                //根节点的添加独立子节点  
                XmlElement person = doc.CreateElement("Person");
                person.SetAttribute("id", "1");
                person.AppendChild(getChildNode(doc, "Name", "FlyElephant"));
                person.AppendChild(getChildNode(doc, "Age", "24"));
                root.AppendChild(person);
    
                //根节点的添加独立子节点  
                person = doc.CreateElement("Person");
                person.SetAttribute("id", "2");
                person.AppendChild(getChildNode(doc, "Name", "keso"));
                person.AppendChild(getChildNode(doc, "Age", "25"));
                root.AppendChild(person);
    
                doc.Save("person.xml");
                Console.WriteLine("创建成功");
    

     XML文件的读取

    C#中读取XML有三种方式,XmlDocument,XmlTextReader,Linq to Xml,由于本人常用的是XmlDocument方法,其他的方法,有兴趣的可以自己尝试一下,看下查询的实现:

       XmlDocument doc = new XmlDocument();
                doc.Load("person.xml");    //加载Xml文件  
                XmlElement root = doc.DocumentElement;   //获取根节点  
                XmlNodeList personNodes = root.GetElementsByTagName("Person"); //获取Person子节点集合  
                foreach (XmlNode node in personNodes)
                {
                    string id = ((XmlElement)node).GetAttribute("id");   //获取Name属性值  
                    string name = ((XmlElement)node).GetElementsByTagName("Name")[0].InnerText;  //获取Age子XmlElement集合  
                    string age = ((XmlElement)node).GetElementsByTagName("Age")[0].InnerText;
                    Console.WriteLine("编号:" + id + "姓名:" + name + "年龄:" + age);
                }
    

      结果如下:

    XML添加

    XML存放的是数据结果跟类相似,如果业务需要往里面动态添加数据,这个时候也需要个人控制一下,代码如下:

               XmlDocument doc = new XmlDocument();
                doc.Load("person.xml");
                XmlElement root = doc.DocumentElement;
                //根节点的添加独立子节点  
                XmlElement person = doc.CreateElement("Person");
                person.SetAttribute("id", "3");
                person.AppendChild(getChildNode(doc, "Name", "Elephant"));
                person.AppendChild(getChildNode(doc, "Age", "23"));
                root.AppendChild(person);
                doc.Save("person.xml");
                Console.WriteLine("XML文件中节点添加成功");

     这个时候XML文件已经发生变化:

    <?xml version="1.0" encoding="UTF-8"?>
    <Persons>
      <Person id="1">
        <Name>FlyElephant修改</Name>
        <Age>24</Age>
      </Person>
      <Person id="2">
        <Name>keso</Name>
        <Age>25</Age>
      </Person>
      <Person id="3">
        <Name>Elephant</Name>
        <Age>23</Age>
      </Person>
    </Persons>
    

     XML修改

    修改其中的一个节点的话,最简单的就是遍历,遍历的时候修改自己想要修改的元素:

       XmlDocument doc = new XmlDocument();
                doc.Load("person.xml");    //加载Xml文件  
                XmlElement root = doc.DocumentElement;   //获取根节点  
                XmlNodeList personNodes = root.GetElementsByTagName("Person"); //获取Person子节点集合
                foreach (XmlNode node in personNodes)
                {
                    XmlElement ele = (XmlElement)node;
                    if (ele.GetAttribute("id") == "3")
                    {
                        XmlElement nameEle = (XmlElement)ele.GetElementsByTagName("Name")[0];
                        nameEle.InnerText = nameEle.InnerText + "修改";
                    }
                }
                Console.WriteLine("节点修改成功");
                doc.Save("person.xml");

      当然如果XML文件中内容很多的话,这种方式就显得的不是那么的合理,可以修改一下一上代码

                XmlElement selectEle = (XmlElement)root.SelectSingleNode("/Persons/Person[@id='1']");
                XmlElement nameEle = (XmlElement)selectEle.GetElementsByTagName("Name")[0];
                nameEle.InnerText = nameEle.InnerText + "修改";

    XML删除

    经过上面的操作,删除节点就很简单的,代码如下:

                XmlDocument doc = new XmlDocument();
                doc.Load("person.xml");    //加载Xml文件  
                XmlElement root = doc.DocumentElement;   //获取根节点  
                XmlNodeList personNodes = root.GetElementsByTagName("Person"); //获取Person子节点集合  
                XmlNode selectNode =root.SelectSingleNode("/Persons/Person[@id='1']");
                root.RemoveChild(selectNode);
                Console.WriteLine("节点删除成功");
                doc.Save("person.xml");

      周末看博客的都是强人,大家周末愉快~

  • 相关阅读:
    Javascript调用C#后台方法及JSon解析
    ul 仿 table 循环滚动
    windows服务
    simple demo how to get the list of online users
    IIS 4.0配置
    在线
    C学习笔记-多源文件的编译
    C学习笔记-多源文件的编译
    C学习笔记-函数
    C学习笔记-函数
  • 原文地址:https://www.cnblogs.com/xiaofeixiang/p/4067791.html
Copyright © 2011-2022 走看看