English(beta)
hfyb的Blog 页面正在加载中 .....

用 System.Xml 读写XML 整理文档

引自 http://www.cnblogs.com/surfsky/archive/2007/03/13/673625.html


example
    <?xml version="1.0" encoding="utf-8"?>
    <LinkLibrary xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Link Cat="aa" Url="aa" Desc="aa" />
      <Link Cat="bb" Url="aa" Desc="aa" />
      <Link Cat="cc" Url="aa" Desc="aa" />
      <Link Cat="aa" Url="aa" Desc="aa" />
      <Link Cat="aa" Url="aa" Desc="aa" />
      <Link Cat="aa" Url="aa" Desc="aa" />
      <Link Cat="aa" Url="aa" Desc="aa" />
      <Link Cat="aa" Url="aa" Desc="aa" />
      <Link Cat="aa" Url="aa" Desc="aa" />
      <Link Cat="aa" Url="aa" Desc="aa" />
    </LinkLibrary>


XML概念
    Root        XML根节点,只能且必须有一个(以上为LinkLibrary)
    Element     节点元素(如Link)
    Attribute   节点属性(如Cat, Url, Desc)
    Content     内容(非空白文本、CDATA、Element、EndElement、EntityReference 或 EndEntity)节点

 

System.Xml空间
    以下类适合快速流式读写XML文件(注:DOM适合随机读写)
    XmlReader, XmlWriter,
    XmlTextReader, XmlTextWriter
    XmlValidatingReader, XmlValidatingWriter    添加了DTD和模式验证,因此提供了数据的有效性验证
    XmlNodeReader, XmlNodeWriter                把XmlNode作为其源


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


------------------------------------------------------------------------
使用XmlTextReader快速读取
------------------------------------------------------------------------
创建
    打开            XmlTextReader reader = new XmlTextReader("*.xml");
    关闭            reader.Close();

属性
    常用
        HasAttributes            获取一个值,该值指示当前节点是否有任何属性。(从 XmlReader 继承。)
        HasValue                 获取一个值,该值指示当前节点是否可以具有非 String.Empty 的 Value。
        AttributeCount           获取当前节点上的属性数。
        Value                    获取当前节点的文本值。
        ValueType                获取当前节点的公共语言运行库 (CLR) 类型。(从 XmlReader 继承。)
        Name                     获取当前节点的限定名。
    其它
        BaseURI                  获取当前节点的基 URI。
        CanReadBinaryContent     获取一个值,该值指示 XmlTextReader 是否实现二进制内容读取方法。
        CanReadValueChunk        获取一个值,该值指示 XmlTextReader 是否实现 ReadValueChunk 方法。
        CanResolveEntity         获取一个值,该值指示此读取器是否可以分析和解析实体。
        Depth                    获取 XML 文档中当前节点的深度。
        Encoding                 获取文档的编码方式。
        EntityHandling           获取或设置一个值,该值指定读取器如何处理实体。
        EOF                      获取一个值,该值指示读取器是否定位在流的结尾。
        IsDefault                获取一个值,该值指示当前节点是否是从 DTD 或架构中定义的默认值生成的属性。
        IsEmptyElement           获取一个值,该值指示当前节点是否为空元素(例如,<MyElement/>)。
        Item                     当在派生类中被重写时,获取此属性的值。(从 XmlReader 继承。)
        LineNumber               获取当前行号。
        LinePosition             获取当前行位置。
        LocalName                获取当前节点的本地名称。
        Namespaces               获取或设置一个值,该值指示是否进行命名空间支持。
        NamespaceURI             获取读取器定位在的节点的命名空间 URI(采用 W3C 命名空间规范中定义的形式)。
        NameTable                获取与此实现关联的 XmlNameTable。
        NodeType                 获取当前节点的类型。
        Normalization            获取或设置一个值,该值指示是否正常化空白和属性值。
        Prefix                   获取与当前节点关联的命名空间前缀。
        ProhibitDtd              获取或设置一个值,该值指示是否允许 DTD 处理。
        QuoteChar                获取用于括起属性节点值的引号字符。
        ReadState                获取读取器的状态。
        SchemaInfo               获取作为架构验证结果分配给当前节点的架构信息。(从 XmlReader 继承。)
        Settings                 获取用于创建此 XmlTextReader 实例的 XmlReaderSettings 对象。
        WhitespaceHandling       获取或设置一个值,该值指定如何处理空白。
        XmlLang                  获取当前 xml:lang 范围。
        XmlResolver              设置用于解析 DTD 引用的 XmlResolver。
        XmlSpace                 获取当前 xml:space 范围。

方法
    定位           
        MoveToAttribute            移动到指定的属性。 
        MoveToContent              检查当前节点是否是内容节点, 如果此节点不是内容节点,则读取器向前跳至下一个内容节点或文件结尾。
        MoveToElement              移动到包含当前属性节点的元素
        MoveToFirstAttribute       移动到第一个属性
        MoveToNextAttribute      移动到下一个属性
        Skip                                跳过当前节点的子级。 
        IsStartElement                测试当前内容节点是否是开始标记。
 
    基本读取           
        Read                       读取一个节点
        ReadString                 将元素或文本节点的内容读取为一个字符串。


    读取特定类型
        ReadBase64                 对 Base64 进行解码并返回解码的二进制字节。
        ReadBinHex                 对 BinHex 进行解码并返回解码后的二进制字节
        ReadChars                  将元素的文本内容读入字符缓冲区。通过连续调用此方法,可以读取大的嵌入文本流。
        ReadValueChunk             读取嵌入在 XML 文档中的大量文本流。 (从 XmlReader 继承。)
        ReadInnerXml               将内部XML节点内容读到一个字符串中
        ReadOuterXml               所有XML节点内容(包括自身节点)

    读取节点
        ReadStartElement           检查当前节点是否为元素并将读取器推进到下一个节点。
        ReadElementString          这是一个用于读取简单纯文本元素的 Helper 方法。
        ReadAttributeValue         将属性值分析为一个或多个 Text、EntityReference 或 EndEntity 节点
        ReadEndElement             检查当前内容节点是否为结束标记并将读取器推进到下一个节点。
        GetAttribute                   获取属性的值。 

    读取内容并转化
        ReadContentAs...           将内容作为指定类型的对象读取。 (从 XmlReader 继承。)
        ReadElementContentAs...    读取当前元素,并将内容作为指定类型的对象返回。 (从 XmlReader 继承。)

    追溯读取
        ReadToFollowing            一直读取,直到找到具有指定限定名的元素
        ReadToDescendant           让 XmlReader 前进到下一个匹配的子代元素。
        ReadToNextSibling          让 XmlReader 前进到下一个匹配的同级元素。
        GetRemainder               获取已缓冲的 XML 剩余部分。
        ReadSubtree                返回新的 XmlReader 实例,此实例可用于读取当前节点及其所有子节点。
 

 

 

------------------------------------------------------------------------
使用XmlTextWriter快速写入
------------------------------------------------------------------------
开闭           
    XmlTextWriter writer = new XmlTextWriter(@"c:\mywriter.xml", null);
    writer.Close();
   
起止XML文档 (<?xml version="1.0"?>
    writer.WriteStartDocument();
    writer.EndDocument();

声明XML格式
    writer.Formatting  = Formatting.Indented;
    writer.Indentation = 缩进字符数
    writer.IndentChar  = 缩进字符
    writer.QuoteChar   = 单引号|双引号

输出注释 (<!-- comment text -->)
    writer.WriteComment("comment text");

输出元素 (<Element>ElementVal</Element>)
    writer.WriteElementString("Element", "ElementVal");
    或者
    writer.StartElement("Element");
    writer.WriteString("ElementVal");
    writer.EndElement();

输出元素属性 (<Element Property="PropertyVal">ElementVal</Element>)
    writer.StartElement("Element");
    writer.WriteAttributeString("Property", "PropertyVal");
    writer.WriteString("ElementVal");
    writer.EndElement();

输出CDATA (<!CDATA>....</CDATA>
    WriteCData("....")

输出字符缓冲区文本
    WriteChars(char[], startPos, length)
   
   

------------------------------------------------------------------------
读例程
------------------------------------------------------------------------
XML文件
    <!-- sample xml file -->
    <bookstore>
      <book genre='novel' ISBN='10-861003-324'>
        <title>The Handmaid's Tale</title>
        <price>19.95</price>
      </book>
      <book genre='novel' ISBN='1-861001-57-5'>
        <title>Pride And Prejudice</title>
        <price>24.95</price>
      </book>
    </bookstore>


取每个书节点上的ISBN属性
    using (XmlReader reader = XmlReader.Create("books.xml"))
    {
        reader.ReadToFollowing("book");
        do {
           Console.WriteLine("ISBN: {0}", reader.GetAttribute("ISBN"));
        } while (reader.ReadToNextSibling("book"));
    }

跳到子节点
    using (XmlReader reader = XmlReader.Create("2books.xml"))
    {
        reader.MoveToContent();                 // bookstore
        reader.ReadToDescendant("book");        // 第一个book
        reader.Skip(); //Skip the first book.   // 第二个book
        ...
    }
   
挖到子节点
    using (XmlReader reader = XmlReader.Create("book3.xml"))
    {
        reader.Read();
        reader.ReadStartElement("book");
            reader.ReadStartElement("title");
                Console.WriteLine("The content of the title element:  {0}", reader.ReadString());
            reader.ReadEndElement();
            reader.ReadStartElement("price");
                Console.WriteLine("The content of the price element:  {0}", reader.ReadString());
            reader.ReadEndElement();
        reader.ReadEndElement();
    }

 

读到指定位置  
   FileStream fs = new FileStream("..\\..\\..\\books.xml", FileMode.Open);
   XmlTextReader tr = new XmlTextReader(fs);
   while(!tr.EOF)
   {
      // 查找类型为XmlNodeType.Element和名称为title的节点
      if(tr.MoveToContent() == XmlNodeType.Element && tr.Name=="title")
      {
         listBox1.Items.Add(tr.ReadElementString());
      }
      else
      {
         tr.Read();
      }
    }

遍历元素属性
   string fileName = "..\\..\\..\\books.xml";
   XmlTextReader tr = new XmlTextReader(fileName);
   while(tr.Read())
   {
      //check to see if it's a NodeType element
      if(tr.NodeType == XmlNodeType.Element) 
      {
         //if it's an element, then let's look at the attributes.
         for(int i = 0; i < tr.AttributeCount; i++) {
            listBox1.Items.Add(tr.GetAttribute(i));
      }
}

   
------------------------------------------------------------------------
写例程
------------------------------------------------------------------------
     XmlTextWriter writer = new XmlTextWriter (filename, null);

     //Use indenting for readability.
     writer.Formatting = Formatting.Indented;

     //xml声明(Write the XML delcaration. )
     writer.WriteStartDocument();

     //预处理指示(Write the ProcessingInstruction node.)
     String PItext="type='text/xsl' href='book.xsl'";
     writer.WriteProcessingInstruction("xml-stylesheet", PItext);

     //文档类型(Write the DocumentType node.)
     writer.WriteDocType("book", null , null, "<!ENTITY h 'hardcover'>");
       
     //注释(Write a Comment node.)
     writer.WriteComment("sample XML");
   
     //根元素(Write a root element.)
     writer.WriteStartElement("book");

     //属性值(Write the genre attribute.)
     writer.WriteAttributeString("genre", "novel");
   
     //属性值(Write the ISBN attribute.)
     writer.WriteAttributeString("ISBN", "1-8630-014");

     //Write the title.
     writer.WriteElementString("title", "The Handmaid's Tale");
             
     //Write the style element.
     writer.WriteStartElement("style");
     writer.WriteEntityRef("h");
     writer.WriteEndElement();

     //文本元素节点(Write the price.)
     writer.WriteElementString("price", "19.95");

     //[CDATA]
     writer.WriteCData("Prices 15% off!!");

     //Write the close tag for the root element.
     writer.WriteEndElement();
            
     writer.WriteEndDocument();

     //Write the XML to file and close the writer.
     writer.Flush();
     writer.Close(); 

     //Load the file into an XmlDocument to ensure well formed XML.
     XmlDocument doc = new XmlDocument();
     //Preserve white space for readability.
     doc.PreserveWhitespace = true;
     //Load the file.
     doc.Load(filename); 
   
     //Display the XML content to the console.
     Console.Write(doc.InnerXml); 


------------------------------------------------------------------------
带验证的读写
------------------------------------------------------------------------
    books.xml
        <?xml version='1.0'?>
        <!-- This file represents a fragment of a book store inventory database -->
        <bookstore xmlns="x-schema:books.xdr">
           <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
              <title>The Autobiography of Benjamin Franklin</title>
              <author>
                 <first-name>Benjamin</first-name>
                 <list-name>Franklin</last-name>
              </author>
              <price>8.99</price>
           </book>
           ...
        </bookstore>
    books.xdr
        <?xml version="1.0"?>
        <Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes">
           <ElementType FTEL="first-name" content="textOnly"/>
           <ElementType FTEL="last-name" content="textOnly"/>
           <ElementType FTEL="name" content="textOnly"/>
           <ElementType FTEL="price" content="textOnly" dt:type="fixed.14.4"/>
           <ElementType FTEL="author" content="eltOnly" order="one">
              <group order="seq">
                 <element type="name"/>
              </group>
              <group order="seq">
                 <element type="first-name"/>
                 <element type="last-name"/>
              </group>
           </ElementType>
           <ElementType FTEL="title" content="textOnly"/>
           <AttributeType FTEL="genre" dt:type="string"/>
           <ElementType FTEL="book" content="eltOnly">
              <attribute type="genre" required="yes"/>
              <element type="title"/>
              <element type="author"/>
              <element type="price"/>
           </ElementType>
           <ElementType FTEL="bookstore" content="eltOnly">
              <element type="book"/>
           </ElementType>
        </Schema>
    cs
        using System.Xml.Schema;
        protected void button1_Click (object sender, System.EventArgs e)
        {
           //change this to match your path structure.
           string fileName = "..\\..\\..\\booksVal.xml";
           XmlTextReader tr = new XmlTextReader(fileName);
           XmlValidatingReader trv = new XmlValidatingReader(tr);
        
           //Set validation type
           trv.ValidationType=ValidationType.XDR;
           //Add in the Validation eventhandler
           trv.ValidationEventHandler += new ValidationEventHandler(this.ValidationEvent);
        
           //Read in node at a time       
           while(trv.Read()) 
           {
              if(trv.NodeType == XmlNodeType.Text)
              listBox1.Items.Add(trv.Value);
           }
        }
        public void ValidationEvent (object sender, ValidationEventArgs args)
        {
           MessageBox.Show(args.Message);
        }

posted on 2007-03-16 19:26  hfyb  阅读(1331)  评论(0编辑  收藏  举报

导航