zoukankan      html  css  js  c++  java
  • C# XML操作

    In the previous chapter, we wrote XML using the XmlWriter class. However, for some situations, especially when doing updates of existing XML, using the XmlDocument class can come in handy. You should be aware of the higher memory consumption though, mainly for large XML documents. Here is some code:

    using System;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace WritingXml
    {
        class Program
        {
            static void Main(string[] args)
            {
                XmlDocument xmlDoc = new XmlDocument();
                XmlNode rootNode = xmlDoc.CreateElement("users");
                xmlDoc.AppendChild(rootNode);
    
                XmlNode userNode = xmlDoc.CreateElement("user");
                XmlAttribute attribute = xmlDoc.CreateAttribute("age");
                attribute.Value = "42";
                userNode.Attributes.Append(attribute);
                userNode.InnerText = "John Doe";
                rootNode.AppendChild(userNode);
    
                userNode = xmlDoc.CreateElement("user");
                attribute = xmlDoc.CreateAttribute("age");
                attribute.Value = "39";
                userNode.Attributes.Append(attribute);
                userNode.InnerText = "Jane Doe";
                rootNode.AppendChild(userNode);
    
                xmlDoc.Save("test-doc.xml");
            }
        }
    }
    

    And the resulting XML:

    <users>
      <user age="42">John Doe</user>
      <user age="39">Jane Doe</user>
    </users>
    

    As you can see, this is a bit more object oriented than the XmlWriter approach, and it does require a bit more code. What we do is that we instantiate an XmlDocument object, which we will use to create both new elements and attributes, using the CreateElement() and CreateAttribute() methods. Each time we do that, we append the elements, either directly to the document, or to another element. You can see this in the example, where the root element ("users") is appended directly to the document, while the user elements are appended to the root element. Attributes are of course appended to the elements where they belong, using the Append() method on the Attributes property. The entire XML document is written to the disk on the last line, where we use the Save() method. 

    Now, as already mentioned, this requires a bit more code than when using the XmlWriter, but imagine a situation where you just need to go into an existing XML document and change a few values. Using the XmlWriter approach, you would have to first read all the information using an XmlReader, store it, change it, and then write the entire information back using the XmlWriter. Because the XmlDocument holds everything in memory for you, updating an existing XML file becomes a lot simpler. The following example opens the "test-doc.xml" file that we created in the previous chapter and increases every user's age by one. See how easy it is:

    using System;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace WritingXml
    {
        class Program
        {
            static void Main(string[] args)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("test-doc.xml");
                XmlNodeList userNodes = xmlDoc.SelectNodes("//users/user");
                foreach(XmlNode userNode in userNodes)
                {
                    int age = int.Parse(userNode.Attributes["age"].Value);
                    userNode.Attributes["age"].Value = (age + 1).ToString();
                }
                xmlDoc.Save(@"D:test-doc.xml");           
            }
        }
    }
    

    We load the XML file and ask for all the <user> nodes. We then iterate through them, read the age attribute into an integer variable, and then we write the value back to the node and attribute, after increasing the value by 1. At last, we save the document back to the same file and if you open it up, you will see that our users all just had a birthday. Pretty cool!

  • 相关阅读:
    将排好序的DataView转成DataTable的方法
    python 备份文件
    UBuntu下安装mysql & 开启远程
    bat 读取文本内容用法
    设置DataGridView控件DataGridViewComboBoxColumn下拉框默认值
    用PS去除图片中文字的6个方法
    asp.net 给Button控件设置背景图片
    windows下架设SVN服务器并设置开机启动
    如何设置svn服务器端的服务开机启动
    C#winform中Excel电子表格导入数据库示例
  • 原文地址:https://www.cnblogs.com/xpvincent/p/4103760.html
Copyright © 2011-2022 走看看