zoukankan      html  css  js  c++  java
  • XmlGeneral示例

    1. Books.xml内容

    books.xml
    <?xml version="1.0" encoding="utf-8" ?>
    <catalog>
    <book id="bk101">
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <genre>Computer</genre>
    <price>44.95</price>
    <publish_date>2000-10-01</publish_date>
    <description>
    An in-depth look at creating applications
    with XML.
    </description>
    </book>
    <book id="bk102">
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
    <publish_date>2000-12-16</publish_date>
    <description>
    A former architect battles corporate zombies,
    an evil sorceress, and her own childhood to become queen
    of the world.
    </description>
    </book>
    </catalog>

    2. 使用XmlTextReader读取xml文件

    XmlTextReader是一个阅读器指针,它总是向前移动,读出一块数据到内存缓冲器。比起XmlDocument类加载整个文档到内存有更好的性能。

        void ReadByXmlTextReader()
        {
            XmlTextReader reader = new XmlTextReader("Books.xml");
            reader.WhitespaceHandling = WhitespaceHandling.None;
    
            while (reader.Read()) {
                if (reader.Name == "book") {
                    Console.WriteLine(reader.GetAttribute("id") + ": ");
    
                    reader.Read();
                    string author = reader.ReadElementContentAsString();
                    string title = reader.ReadElementContentAsString();
                    string genre = reader.ReadElementContentAsString();
                    string price = reader.ReadElementContentAsString();
                    string publishDate = reader.ReadElementContentAsString();
                    string description = reader.ReadElementContentAsString();
    
                    Console.WriteLine("{0} book {1} written by {2}, published on {3}", genre, title, author, publishDate);
                    Console.WriteLine(description);
                }
            }
    
            reader.Close();
        }
    
    

    3.使用XmlDocument和XmlNodeReader读取文件

    XmlNodeReader类似于XmlTextReader,但是接受一个XmlNode实例作为读出目标。

        void ReadByXmlDocument()
        {
            var doc = new XmlDocument();
            doc.Load("Books.xml");
    
            var nodes = doc.GetElementsByTagName("book");
            foreach (XmlNode node in nodes) {
                Console.WriteLine(node.Attributes["id"].Value + ":");
    
                var reader = new XmlNodeReader(node);
                reader.Read();
                reader.Read();
    
                string author = reader.ReadElementContentAsString();
                string title = reader.ReadElementContentAsString();
                string genre = reader.ReadElementContentAsString();
                string price = reader.ReadElementContentAsString();
                string publishDate = reader.ReadElementContentAsString();
                string description = reader.ReadElementContentAsString();
    
                Console.WriteLine("{0} book {1} written by {2}, published on {3}", genre, title, author, publishDate);
                Console.WriteLine(description);
            }
    
        }
    
    

    4.修改Xml文件 

    XmlLDocument没有Close或者Dispose方法,因为它是XML文档的内存表现。一旦读出,文件不再被需要。

        void ChangeXmlDocument()
        {
            var xmlDocument = new XmlDocument();
            xmlDocument.Load("Books.xml");
    
            XmlNode nodeToModify = xmlDocument.DocumentElement.SelectSingleNode("book/genre");
            nodeToModify.InnerText = "XML Tech";
    
            XmlElement newElement = xmlDocument.CreateElement("book");
    
            XmlAttribute newAttribute = xmlDocument.CreateAttribute("id");
            newAttribute.Value = "bk103";
            newElement.Attributes.Append(newAttribute);
    
            XmlElement authorElement = xmlDocument.CreateElement("author");
            authorElement.InnerText = "Mark Russinovich,David Solomon,Alex Ionecsu";
            newElement.AppendChild(authorElement);
    
            XmlElement titleElement = xmlDocument.CreateElement("title");
            titleElement.InnerText = "Windows Internals, 5th edition";
            newElement.AppendChild(titleElement);
    
            XmlElement genreElement = xmlDocument.CreateElement("genre");
            genreElement.InnerText = "Windows Server 2008";
            newElement.AppendChild(genreElement);
    
            XmlElement priceElement = xmlDocument.CreateElement("price");
            priceElement.InnerText = "69.99";
            newElement.AppendChild(priceElement);
    
            XmlElement publishDateElement = xmlDocument.CreateElement("publish_date");
            publishDateElement.InnerText = "2009-6-17";
            newElement.AppendChild(publishDateElement);
    
            XmlElement descriptionElement = xmlDocument.CreateElement("description");
            descriptionElement.InnerText = "Windows Internals...";
            newElement.AppendChild(descriptionElement);
    
            xmlDocument.DocumentElement.AppendChild(newElement);
    
            xmlDocument.Save("Books.xml");
    
        }
    
    

  • 相关阅读:
    函数指针作为函数參数,实现冒泡排序的升序排序和降序排序
    为什么通过空指针(NULL)能够正确调用类的部分成员函数
    vc6.0 点编译时提示Cannot complile the file &#39;D:souce-codevc-workspace对话框MainFrm.h&#39;; no compile tool is
    struts2中Action訪问servlet的两种方式
    删除LINUX更新后多余的内核
    cocos2d-x 3.0rc2版公布了
    The user specified as a definer (&#39;root&#39;@&#39;%&#39;) does not exist
    HDU 4287 Intelligent IME(map运用)
    HDU 4925 Apple Tree(推理)
    Linux下使用Fastboot给手机刷ROM
  • 原文地址:https://www.cnblogs.com/cuishengli/p/1901628.html
Copyright © 2011-2022 走看看