流模型 操作 xml

xml文件:

<bookstore>
  <book genre="autobiography" publicationdate="1991" ISBN="1-861003-11-0">
      <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>

</bookstore>

1、

1.使用XmlReader读取XML文档

操作XML文档时,操作的单元是节点,首先要知道XML文档有哪些节点类型..NET,有关XML的类型位于System.Xml.*命名空间下,System.Xml.XmlNodeType枚举列出了XML的节点类型.

  public enum XmlNodeType
    {
        // 摘要:
        //     如果未调用 Read 方法,则由 System.Xml.XmlReader 返回。
        None = 0,
        //
        // 摘要:
        //     元素(例如,<item>)。
        Element = 1,
        //
        // 摘要:
        //     属性(例如,id='123')。
        Attribute = 2,
        //
        // 摘要:
        //     节点的文本内容。
        Text = 3,
        //
        // 摘要:
        //     CDATA 节(例如,<![CDATA[my escaped text]]>)。
        CDATA = 4,
        //
        // 摘要:
        //     实体引用(例如,&num;)。
        EntityReference = 5,
        //
        // 摘要:
        //     实体声明(例如,<!ENTITY...>)。
        Entity = 6,
        //
        // 摘要:
        //     处理指令(例如,<?pi test?>)。
        ProcessingInstruction = 7,
        //
        // 摘要:
        //     注释(例如,<!-- my comment -->)。
        Comment = 8,
        //
        // 摘要:
        //     作为文档树的根的文档对象提供对整个 XML 文档的访问。
        Document = 9,
        //
        // 摘要:
        //     由以下标记指示的文档类型声明(例如,<!DOCTYPE...>)。
        DocumentType = 10,
        //
        // 摘要:
        //     文档片段。
        DocumentFragment = 11,
        //
        // 摘要:
        //     文档类型声明中的表示法(例如,<!NOTATION...>)。
        Notation = 12,
        //
        // 摘要:
        //     标记间的空白。
        Whitespace = 13,
        //
        // 摘要:
        //     混合内容模型中标记间的空白或 xml:space="preserve" 范围内的空白。
        SignificantWhitespace = 14,
        //
        // 摘要:
        //     末尾元素标记(例如,</item>)。
        EndElement = 15,
        //
        // 摘要:
        //     由于调用 System.Xml.XmlReader.ResolveEntity() 而使 XmlReader 到达实体替换的末尾时返回。
        EndEntity = 16,
        //
        // 摘要:
        //     XML 声明(例如,<?xml version='1.0'?>)。
        XmlDeclaration = 17,
    }

XmlReader是一个抽象类,可以通过调用它的静态方法Create()来创建对象,XmlReader是以”节点”为单位进行读取.节点在.NET中由System.Xml.XmlNode类型表示,下面获得节点类型及对应XmlNode类型的Name和Value属性.

  string str="";

  XmlReader rdr = XmlReader.Create("books.xml");
      while (rdr.Read())
      {
        str+=string.Format("{0} | {1} - {2}",reader.NodeType,reader.Name,reader.Value);
      }

while(reader.Read())循环中的代码部分相对简单,有下面几点需要注意:

(1).不是所有的节点都有名称或者值,对于Comment节点来说,只有值没有名称.对于Element接地拿来说,只有名称没有值.

(2)尽管XmlReader是以只读的,前进的方式进行读取,但是对于元素的属性,可以根据所以访问任意属性.

(3)XmlReader具有一个属性ValueType.

XmlReader还有两组强类型方法(注意这里说的是组,说明还有asInt,asFloat,asLong,asObject.),例如:

  ReadContentAsBoolean();   

  ReadElementContentAsBoolean();

它们用于简化操作,省却一些枯燥的类型转换的代码.这两组方法最主要的区别是:ReadContentAsXXX()方法应在Text节点上调用,ReadElementContentAsXXX()方法应在Element节点上调用.这两组方法都会使XmlReader的指针前进到下一节点,效果上相当于调用了Read()方法.

2、使用XmlWriter生成XML文档.

使用XmlWriter生成XML文档也是比较容易的.注意到它具有只写,单向前进的特点,因此使用XmlWriter不能直接修改现有的XML文档,只能用于创建全新的XML文档.如果要使用XmlWriter修改XML,比如修改某个元素的属性值,那么可以配合XmlReader来间接完成.

 

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;

settings.Encoding = System.Text.Encoding.UTF8; 
settings.NewLineOnAttributes = true;
XmlWriter writer = XmlWriter.Create("newbook.xml", settings);
writer.WriteStartDocument();
//Start creating elements and attributes
writer.WriteStartElement("book");
writer.WriteAttributeString("genre", "Mystery");
writer.WriteAttributeString("publicationdate", "2001");
writer.WriteAttributeString("ISBN", "123456789");
writer.WriteElementString("title", "Case of the Missing Cookie");
writer.WriteStartElement("author");
writer.WriteElementString("name", "Cookie Monster");
writer.WriteEndElement();
writer.WriteElementString("price", "9.99");
writer.WriteEndElement();
writer.WriteEndDocument();
//clean up
writer.Flush();
writer.Close();

 

 

posted @ 2017-02-22 11:25  奋发中的彗星  阅读(271)  评论(0编辑  收藏  举报