81375

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?

Answer1:

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.

Answer2:

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.

Recommend

  • Analog of php serialize in c#
  • saving xml file and ConformanceLevel
  • Transform XML using XSLT to SQL
  • How can I generate XML with CR, instead of CRLF in XmlTextWriter
  • Show/Hide div in Bootstrap 4 while resizing height of leaflet map
  • Why does \\0 print different output in different system in java
  • Check thread status while leaving it in a waitable state
  • IE readonly textarea problem
  • How to make a user wait with Laravel
  • Where in the relevant specification is it documented that some comments in a SQL script are, in fact
  • How to override __call__ in celery on main?
  • Generating anchors with PyYAML.dump()?
  • What is the reason that Policy.getPolicy() is considered as it will retain a static reference to the
  • Get Currently Active User in Android
  • Magento site down due to mysql error General error: 1030 Got error -1 from storage engine
  • In matplotlib, how do you change the fontsize of a single figure?
  • Spring: No transaction manager has been configured
  • Updating both a ConcurrentHashMap and an AtomicInteger safely
  • accepts_nested_attributes_for practical form use for in Rails 3
  • Object and struct member access and address offset calculation
  • What's the purpose of QString?
  • Xcode 4 NSLog Macro link in Xcode 3
  • Play WS (2.2.1): post/put large request
  • Projection media query: browser support and workarounds?
  • Deserializing XML into class C#
  • Trying to switch camera back to front but getting exception
  • How can I use Kendo UI with Razor?
  • Is there a mandatory requirement to switch app.yaml?
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Why joiner is not used after Sequence generator or Update statergy
  • How do I configure my settings file to work with unit tests?
  • File not found error Google Drive API
  • Qt: Run a script BEFORE make
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Programmatically clearing map cache
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML