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!

  • 相关阅读:
    centos7下vi的用法
    jQuery的hover方法搭配css的hover选择器,实现选中元素突出显示
    CSS3弹性伸缩布局(下)——flex布局
    什么是架构设计
    界面底部Tab实现
    一行代码实现圆形头像
    免费快速制作个人简历
    基于JS的问卷调查
    基于CSS的个人网页
    纯HTML课表
  • 原文地址:https://www.cnblogs.com/xpvincent/p/4103760.html
Copyright © 2011-2022 走看看