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");
    
        }
    
    

  • 相关阅读:
    PHP——文件操作
    PHP——注册页面,审核页面,登录页面:加Session和Cookie
    ajax——优化0126(增删改查:添加查看详情,返回结果类型为JSON型,在窗口显示)
    ajax——三级联动下拉列表框的优化(简化页面,用jquery插件代替原来页面代码,返回处理数据类型为"TEXT")
    ajax——实现三级联动下拉列表
    通过view实现字段的只读、隐藏操作【转】
    OpenERP how to set the tree view limit
    OpenERP 疑问之一
    Django 安装
    OpenERP 中国财务模块 调整
  • 原文地址:https://www.cnblogs.com/cuishengli/p/1901628.html
Copyright © 2011-2022 走看看