c# 里使用XmlWriter XmlTextWriter XmlReader XmlTextReader

当读写一个xml文件的时候,你是考虑下面的哪个呢?

XmlWriter XmlTextWriter XmlReader XmlTextReader

 

下面是别人的一个回答:

That is a good question,I usually use XMLTextWriter and XMLTextReader but I didn't care about the difference between XMLTextWriter/XMLWriter or XMLTextReader/XMLReader,I check on msdn and noticed that XMLTextWriter is actually implement XMLWriter (which means that XMLTextWriter has all the features of? XMLWriter and more) and so with XMLTextReader and XMLReader

Microsoft said and I quote:

Although the Microsoft .NET Framework includes the XmlTextWriter class, which is an implementation of the XmlWriter class, in the 2.0 release, it is recommended that you use the Create method to create new XmlWriter objects. The Create method allows you to specify the features to support on the created XmlWriter object, and it also allows you to take full advantage of the new features introduced in the 2.0 release.

This is the link I quoted from:

http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter(VS.80).aspx

XmlTextReader Class:Reads character streams. It is a forward-only reader that has methods that return data on content and node types. There is no Document Type Definition (DTD) or schema support.

Here is the link:http://msdn.microsoft.com/en-us/library/aa720470(VS.71).aspx

XmlTextWriter maintains a namespace stack corresponding to all the namespaces defined in the current element stack. Using XmlTextWriter you can declare namespaces manually.

XmlTextWriter also allows you to override the current namespace declaration

Here is the link :http://msdn.microsoft.com/en-us/library/system.xml.xmltextwriter.aspx

And here are other usefull links on whole thing:

http://msdn.microsoft.com/en-us/library/tfz3cz6w(VS.80).aspx

http://msdn.microsoft.com/en-us/library/9d83k261(VS.80).aspx

 

相比较而言,XmlWriter 在创建一个xml file的时候,有一定的优势。它可以指定具体的XmlSettings, 但是XmlTextWriter 没有这个重载方法。使用的时候还应该注意XmlWriter是一个抽象基类。

 

XmlWriter 类是一个抽象基类,提供只进、只写、非缓存的方式来生成 XML 流。可以用于构建符合 W3C 可扩展标记语言 (XML) 1.0(第二版)(www.w3.org/TR/2000/REC-xml-20001006.html) 建议和 XML 中的命名空间建议 (www.w3.org/TR/REC-xml-names/) 的 XML 文档。

 

使用XmlTextWriter:

using System;

using System.IO;

using System.Xml;

public class Sample

{

  public static void Main()

  {

     XmlTextWriter writer = new XmlTextWriter("titles.xml", null);

     //写入根元素

     writer.WriteStartElement("items");

     //加入子元素

     writer.WriteElementString("title", "Unreal Tournament 2003");

     writer.WriteElementString("title", "C&C: Renegade");

     writer.WriteElementString("title", "Dr. Seuss's ABC");

     //关闭根元素,并书写结束标签

     writer.WriteEndElement();

     //将XML写入文件并且关闭XmlTextWriter

     writer.Close();

  }

}

使用XmlWriter:

using System;

using System.IO;

using System.Xml;

public class Sample

{

  public static void Main()

  {

          // 优势体现在这里
           XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = false;
            settings.NewLineOnAttributes = false;

            XmlWriter writer = XmlWriter.Create("booknew.xml", settings);

     //写入根元素

     writer.WriteStartElement("items");

     //加入子元素

     writer.WriteElementString("title", "Unreal Tournament 2003");

     writer.WriteElementString("title", "C&C: Renegade");

     writer.WriteElementString("title", "Dr. Seuss's ABC");

     //关闭根元素,并书写结束标签

     writer.WriteEndElement();

     //将XML写入文件并且关闭XmlTextWriter

     writer.Close();

  }

}

原文地址:https://www.cnblogs.com/herbert/p/1770109.html