虎克的博客

Enthusiasm Biogeography-Biodiversity Informatics-Data Sciences

XML文档的读写

| Comments

(一)XML Readers和Writers类

(二)什么时候使用XmlReader和XmlWriter类

XmlReader类

? You need to only read the document.
? The document is huge.
? You need to keep the memory footprint small.
? You want to work with many XML documents that are a reasonable size.
? You do not want to access various parts of the document randomly.
XmlWriter类
? You want to only write content.
? You want to keep the memory footprint small.
? You are writing huge XML documents and looking for better performance.

(三)Reader类

  • XmlTextReader类

使用XmlTextReader类打开XML文档:

2009-05-09_00012

   1:  using System;

   2:  using System.Xml;

   3:  using System.IO;

   4:  using System.Text;

   5:   

   6:  public partial class Default7 : System.Web.UI.Page

   7:  {

   8:      protected void Page_Load(object sender, EventArgs e)

   9:      {

  10:   

  11:      }

  12:      protected void Button1_Click(object sender, EventArgs e)

  13:      {

  14:          XmlTextReader reader;

  15:          if (RadioButton1.Checked)

  16:          {

  17:              reader = new XmlTextReader(TextBox1.Text);

  18:          }

  19:          if (RadioButton2.Checked)

  20:          {

  21:              FileStream stream = File.OpenRead(TextBox1.Text);

  22:              reader = new XmlTextReader(stream);

  23:              stream.Close();

  24:              reader.Close();

  25:          }

  26:          if (RadioButton3.Checked)

  27:          {

  28:              MemoryStream ms = new MemoryStream();            

  29:              byte[] data = ASCIIEncoding.ASCII.GetBytes(TextBox1.Text);

  30:              ms.Write(data, 0, data.Length);

  31:              reader = new XmlTextReader(ms);

  32:              //some processing code

  33:              ms.Close();

  34:              reader.Close();

  35:          }

  36:          Response.Write("XML Document Opened Successfully!");

  37:      }

  38:  }

  • XmlValidatingReader类
  • XmlNodeReader类

Comments