What is the purpose of xmltextwriter if xmlwriter is the recommended one

As per MSDN:

Although the Microsoft .NET Framework includes the XmlTextWriter class, which is a concrete implementation of the XmlWriter class, in version 2.0 of the .NET Framework, the recommended practice is to create XmlWriter instances using the Create method.

So if we have to use XmlWriter anyhow what is the purpose of having XmlTextWriter? why do it exist?


XMLWriter is an abstract class which acts as the base type for all XML writers. It was introduced with .NET 1.1. XMLTextWriter is one of many possible implementations of XMLWriter, specifically to create a text representation. It was also introduced with .NET 1.1.

Now, in .NET 2, the XMLWriter class was extended to contain additional static Create methods, e.g. XMLWriter.Create(string) to write the XML to a file. These methods create an object of an internal type that implements XMLWriter (so the actual type it uses is not visible). That way, you don’t have to worry about the actual implementation but can just use the common base type, XMLWriter, to use it.

Those Create methods could replace the previous XMLTextWriter completely, but since the .NET framework wants to remain backwards compatible, it had to keep the XMLTextWriter although the XMLWriter is now capable of handling those cases.


The XmlTextWriter class is one of the implementations of the XmlWriter abstract class.

The text is somewhat misleading, as it's not actually possible to create an instance of the abstract XmlWriter class. When you call the Create method, it will create an instance of one of the classes that inherit from XmlWriter. For example calling Create for a stream will return an instance of the XmlWellFormedWriter class.

The purpose of having a Create method to get an instance, is that it can use different classes that fit best depending on what you are trying to do. You tell the framework to create an instance of an appropriate class, so that you don't have to worry about it.

人吐槽 人点赞



用户名: 密码:
验证码: 匿名发表


查看评论:What is the purpose of xmltextwriter if xmlwriter is the recommended one