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!

  • 相关阅读:
    设置代码ios中根据文本设置label高度设置代码
    程序方法对于UIWindow的认识程序方法
    信息浏览器从Android的浏览器中传递cookie数据到App中信息浏览器
    高度状态栏Android获取状态栏的高度高度状态栏
    网络监听Network Daemon(Android Netd)架构和源码分析网络监听
    文件读取Android02文件的保存与读取文件读取
    地址方法[ios开发]利用有道翻译API实现简单的翻译功能地址方法
    温度调试MAC CPU温度监视及风扇速度控制软件温度调试
    设置打开android开发之快速设置,一键wifi开闭,移动网络开闭,电池设置,飞行模式,………………………………设置打开
    界面更新android 转载 widget点击事件界面更新
  • 原文地址:https://www.cnblogs.com/xpvincent/p/4103760.html
Copyright © 2011-2022 走看看