What is the difference between XmlTextWriter and XmlWriter?
Asked Answered
D

1

29

I am looking at these these two classes in C#: XmlTextWriter and XmlWriter. Can anyone explain the difference and tell me where to use which?

Doloroso answered 1/8, 2011 at 13:54 Comment(6)
Well, first off, you can't instantiate XmlWriter.Aleydis
@Aleydis Hm, technically yes, but it has a static Create() method which creates an instance. Doesn't that count?Fixity
@Marcel: You can only create an instance of a subclass. You can't do new XmlWriter because it's abstract.Aleydis
@Aleydis I just called XmlWriter.Create(stream, settings) right now and it gives me an XmlWriter. It's also described exactly to do so in the docs: msdn.microsoft.com/en-us/library/ms162617%28v=vs.110%29.aspxFixity
@Marcel: What does GetType() on that instance return?Aleydis
@Aleydis In my case, it's a XmlWellFormedWriter. You get the point.Fixity
F
32

XmlWriter is an abstract class.
XmlTextWriter is a specific implementation of XmlWriter.

You should always call XmlWriter.Create.

MSDN says:

In the .NET Framework version 2.0 release, the recommended practice is to create XmlWriter instances using the XmlWriter.Create method and the XmlWriterSettings class. This allows you to take full advantage of all the new features introduced in this release. For more information, see Creating XML Writers.

Fumigate answered 1/8, 2011 at 13:56 Comment(3)
"You should always call XmlWriter.Create" why do you make this assertion ?Galliett
@Steve: Because Microsoft does.Fumigate
Too bad XmlWriter and XmlWriterSettings miss a QuoteChar property which XmlTextWriter has, rendering them useless if you require (for whatever reasons) 'single-quoted' attributes instead of the "double-quoted" ones.Unhandled

© 2022 - 2024 — McMap. All rights reserved.