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/archive/2010/07/02/1770109.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值