Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between XmlTextWriter and XmlWriter?

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?

like image 470
Muhammad Basit Avatar asked Aug 01 '11 13:08

Muhammad Basit


People also ask

What is XmlTextWriter?

XmlTextWriter Represents a writer that provides a fast, non-cached, forward-only way of generating streams or files containing XML data. XmlTextWriter renders XML data to a string. This string remains in the memory of the C# program. XmlTextWriter is useful for in-memory generation of XML.

Which method of XML text writer class is used to put value for attribute of XML?

WriteAttributeString("xmlns","x",null,"abc"); By using the write methods that take a prefix as an argument you can also specify which prefix to use. In the following example, two different prefixes are mapped to the same namespace URI to produce the XML text <x:root xmlns:x="urn:1"><y:item xmlns:y="urn:1"/></x:root> .


1 Answers

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.

like image 116
SLaks Avatar answered Sep 19 '22 23:09

SLaks