.NET Framework Class Library
XmlWriter Class

Represents a writer that provides a fast, non-cached, forward-only means of generating streams or files containing XML data.

Namespace: System.Xml
Assembly: System.Xml (in system.xml.dll)

Syntax

Visual Basic (Declaration)
Public MustInherit Class XmlWriter
    Implements IDisposable
Visual Basic (Usage)
Dim instance As XmlWriter
C#
public abstract class XmlWriter : IDisposable
C++
public ref class XmlWriter abstract : IDisposable
J#
public abstract class XmlWriter implements IDisposable
JScript
public abstract class XmlWriter implements IDisposable
Remarks

The XmlWriter class supports the W3C Extensible Markup Language (XML) 1.0 and the Namespaces in XML recommendations.

NoteNote

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.

For more information about the XmlWriter class, see Writing XML with the XmlWriter.

Security Considerations

The following items are things to consider when working with the XmlWriter class.

  • Exceptions thrown by the XmlWriter can disclose path information that you do not want bubbled up to the application. Your applications must catch exceptions and process them appropriately.

  • The XmlWriter does not validate any data that is passed to the WriteDocType or WriteRaw methods. You should not pass arbitrary data to these methods.

Inheritance Hierarchy

System.Object
  System.Xml.XmlWriter
     System.Xml.XmlTextWriter
     System.Xml.Xsl.Runtime.XmlQueryOutput
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

Version Information

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
See Also

Mots clés :


Community Content

nikpe
Getting XML as String using XmlWriter and StringWriter

When you create a string of Xml using the XmlWriter and StringWriter, the string will have the encoding set at "utf-16" in the first line
i.e.
<?xml version="1.0" encoding="utf-16"?>

This is because StringWriter (http://msdn2.microsoft.com/en-us/library/system.io.stringwriter.aspx) only produces "utf-16" output, as strings are always "utf-16" in .Net

Refer:
Encodings in the .NET Framework
http://www.microsoft.com/globaldev/getWR/steps/wrg_codepage.mspx

"utf-16" Vs "utf-8"

http://www.thescripts.com/forum/thread177860.html

There is no way to get this as in "utf-8" using the StringWriter
i.e. the following is not possible
<?xml version="1.0" encoding="utf-8"?>

Because of this restriction, while creating the XmlWriter, we need to use the XmlWriterSettings and set the encoding to Encoding.Unicode. Note "Unicode" in Encoding.Unicode means "utf-16" as per MS terminology. ( http://msdn2.microsoft.com/en-us/library/system.text.encoding.aspx )

Code sample:

 public static string ConstructXml()
{
StringWriter stringWriter = new StringWriter();
  XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = false; //already the default
settings.ConformanceLevel = ConformanceLevel.Document; //already the default
settings.NewLineOnAttributes = true;
settings.Indent = true;
settings.IndentChars = "\t";
settings.Encoding = Encoding.Unicode; //strings in .NET are by default encoded as "utf-16", "Unicode" in MS terminology is "utf-16"
  XmlWriter writer = XmlWriter.Create(stringWriter, settings);
  //use the different write methods
//writer.WriteAttributes(...)
//writer.WriteAttributeString(...)
//writer.WriteElementString(...)
  //finally construct a string out of it
string xmlString;
xmlString = stringWriter.ToString();
return xmlString;
 }
Mots clés :

Page view tracker