//   在控制台中显示每种类型的数目     
  Console.WriteLine("Total   Comments:"   +   cc.ToString());     
  Console.WriteLine("Total   Attributes:"   +   ac.ToString());     
  Console.WriteLine("Total   Elements:"   +   el.ToString());     
  Console.WriteLine("Total   Entity:"   +   et.ToString());     
  Console.WriteLine("Total   Process   Instructions:"   +   pi.ToString());     
  Console.WriteLine("Total   Declaration:"   +   xd.ToString());     
  Console.WriteLine("Total   DocumentType:"   +   dc.ToString());     
  Console.WriteLine("Total   WhiteSpaces:"   +   ws.ToString());     
  }     
  }     
  }                   
  以上,我向大家介绍了如何运用XmlTextReader类的对象来读取XML文档,并根据节点的NodeType属性来取得其节点类型信息。同时XmlReader这个基类还有XmlNodeReader和XmlValidatingReader等派生类,它们分别是用来读取XML文档的节点和模式的。限于篇幅,这里就不介绍了,读者可以参考有关资料。                 
  四.写XML文档的方法:         
  XmlWriter类包含了写XML文档所需的方法和属性,它是XmlTextWriter类和XmlNodeWriter类的基类。该类包含了WriteNode、WriteString、WriteAttributes、WriteStartElement以及WriteEndElement等一系列写XML文档的方法,其中有些方法是成对出现的。比如你要写入一个元素,你首先得调用WriteStartElement方法,接着写入实际内容,最后是调用WriteEndElement方法以表示结束。该类还包含了WriteState、XmlLang和XmlSpace等属性,其中WriteState属性表明了写的状态。因为XmlWriter类包含了很多写XML文档的方法,所以这里只是介绍最主要的几种。下面我们通过其子类XmlTextWriter类来说明如何写XML文档。         
  首先,我们要创建一个XmlTextWriter类的实例对象。该类的构造函数XmlTextWriter有三种重载形式,其参数分别为一个字符串、一个流对象和一个TextWriter对象。这里我们运用字符串的参数形式,该字符串就指明了所要创建的XML文件的位置,方法如下:         
  XmlTextWriter   textWriter   =   New   XmlTextWriter("C:\\myXmFile.xml",   null);                   
  在创建完对象后,我们调用WriterStartDocument方法开始写XML文档,在完成写工作后,就调用WriteEndDocument结束写过程并调用Close方法将它关闭。在写的过程中,我们可以调用WriteComment方法来添加说明,通过调用WriteString方法来添加一个字符串,通过调用WriteStartElement和WriteEndElement方法对来添加一个元素,通过调用WriteStartAttribute和WriteEndAttribute方法对来添加一个属性。我们还可以通过调用WriteNode方法来添加整一个节点,其它的写的方法还包括WriteProcessingInstruction和WriteDocType等等。下面的实例就是介绍如何具体运用这些方法来完成XML文档的写工作的。         
  using   System;     
  using   System.Xml;         
  namespace   WriteXML     
  {     
  class   Class1     
  {     
  static   void   Main(   string[]   args   )     
  {     
  //   创建XmlTextWriter类的实例对象     
  XmlTextWriter   textWriter   =   new   XmlTextWriter("C:\\myXmFile.xml",   null);         
  //   开始写过程,调用WriteStartDocument方法     
  textWriter.WriteStartDocument();         
  //   写入说明     
  textWriter.WriteComment("First   Comment   XmlTextWriter   Sample   Example");     
  textWriter.WriteComment("myXmlFile.xml   in   root   dir");         
  //   写入一个元素     
  textWriter.WriteStartElement("Name",   "");     
  textWriter.WriteString("Student");     
  textWriter.WriteEndElement();         
  //   再写入一个元素     
  textWriter.WriteStartElement("Address",   "");     
  textWriter.WriteString("Colony");     
  textWriter.WriteEndElement();         
  //   写入字符     
  char   []   ch   =   new   char[3];     
  ch[0]   =   'a';     
  ch[1]   =   'r';     
  ch[2]   =   'c';     
  textWriter.WriteStartElement("Char");     
  textWriter.WriteChars(ch,   0,   ch.Length);     
  textWriter.WriteEndElement();         
  //   写文档结束,调用WriteEndDocument方法     
  textWriter.WriteEndDocument();         
  //   关闭textWriter     
  textWriter.Close();     
  }     
  }     
  }                   
  五.运用XmlDocument类:         
  XmlDocument类的对象代表了一个XML文档,它也是一个非常重要的XML类。该类包含了Load、LoadXml以及Save等重要的方法。其中Load方法可以从一个字符串指定的XML文件或是一个流对象、一个TextReader对象、一个XmlReader对象导入XML数据。LoadXml方法则完成从一个特定的XML文件导入XML数据的功能。它的Save方法则将XML数据保存到一个XML文件中或是一个流对象、一个TextWriter对象、一个XmlWriter对象中。         
  下面的程序中我们用到了XmlDocument类对象的LoadXml方法,它从一个XML文档段中读取XML数据并调用其Save方法将数据保存在一个文件中。         
  //   创建一个XmlDocument类的对象     
  XmlDocument   doc   =   new   XmlDocument();     
  doc.LoadXml(("<Student   type='regular'   Section='B'><Name>Tommy   Lex</Name></Student>"));         
  //   保存到文件中     
  doc.Save("C:\\student.xml");                   
  这里,我们还可以通过改变Save方法中参数,将XML数据显示在控制台中,方法如下:         
  doc.Save(Console.Out);                   
  而在下面的程序中,我们用到了一个XmlTextReader对象,通过它我们读取"books.xml"文件中的XML数据。然后创建一个XmlDocument对象并载入XmlTextReader对象,这样XML数据就被读到XmlDocument对象中了。最后,通过该对象的Save方法将XML数据显示在控制台中。         
  XmlDocument   doc   =   new   XmlDocument();     
  //   创建一个XmlTextReader对象,读取XML数据     
  XmlTextReader   reader   =   new   XmlTextReader("c:\\books.xml");     
  reader.Read();         
  //   载入XmlTextReader类的对象     
  doc.Load(reader);     
  //   将XML数据显示在控制台中     
  doc.Save(Console.Out);                   
  六.总结:         
  XML技术作为.Net的基石,其重要性自然不言而喻。.Net框架包含了五个命名空间和大量的类来支持与XML技术有关的操作。其中System.Xml是最重要的一个命名空间,其中的XmlReader类和XmlWriter类以及它们的派生类完成了XML文档的读写操作,是最基本也是最重要的类。XmlDocument类代表了XML文档,它能完成与整个XML文档相关的各类操作,同时和其相关的XmlDataDocument类也是非常重要的,值得读者的深入研究。                   
  附录         
  "books.xml"文件如下:         
  <?xml   version='1.0'?>     
  <!--   This   file   represents   a   fragment   of   a   book   store   inventory   database   -->     
  <bookstore>     
  <book   genre="autobiography"   publicationdate="1981"   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>     
  <book   genre="novel"   publicationdate="1967"   ISBN="0-201-63361-2">     
  <title>The   Confidence   Man</title>     
  <author>     
  <first-name>Herman</first-name>     
  <last-name>Melville</last-name>     
  </author>     
  <price>11.99</price>     
  </book>     
  <book   genre="philosophy"   publicationdate="1991"   ISBN="1-861001-57-6">     
  <title>The   Gorgias</title>     
  <author>     
  <first-name>Sidas</first-name>     
  <last-name>Plato</last-name>     
  </author>     
  <price>9.99</price>     
  </book>     
  </bookstore>