XmlWriter Class
System.Xml
Implements a non-caching
XmlWriter
that
provides fast, forward-only access to a stream of XML data.
This class is used to create XML documents.
Inheritance
Fields
This class has no fields.
Properties
-
CoreNewLineHandling
protected NewLineHandling CoreNewLineHandling { get; }
Gets the core newline handling setting.
-
FlushOnClose
protected bool FlushOnClose { get; }
Gets a value indicating whether to flush the writer when it is closed.
-
Settings
public XmlWriterSettings Settings { get; }
Gets the
XmlWriterSettings
used to create thisXmlWriter
instance. -
WriteState
public WriteState WriteState { get; }
Gets the current state of the writer.
-
XmlLang
public string XmlLang { get; }
Gets the current
xml:lang
scope. -
XmlSpace
public XmlSpace XmlSpace { get; }
Gets the current
xml:space
scope.
Methods
-
AttributeString
public void AttributeString (string prefix, string localName, string ns, string value)
Writes the attribute string.
-
Close
public abstract void Close ()
Closes this stream and, if
Dispose(bool)
is called withtrue
, flushes the underlying stream. -
Create
public static XmlWriter Create (Stream output)
Creates an
XmlWriter
that can write to the specified stream.Overload for creating with aStream
. -
Create
public static XmlWriter Create (string path)
Creates an
XmlWriter
that can write to the specified file.Overload for creating with a file path. -
Create
public static XmlWriter Create (string filePath, XmlWriterSettings settings)
Creates an
XmlWriter
that can write to the specified file with the given settings. -
Flush
public virtual void Flush ()
Flushes whatever is in the buffer to the underlying stream and checks for the correctness of the XML document.
-
LookupPrefix
public virtual string LookupPrefix (string ns)
Looks up the prefix associated with the given namespace.
-
OnClosing
protected virtual void OnClosing (EventArgs e)
Raises the
Closing
event. -
QuoteChar
protected char QuoteChar { get; }
Gets the character used for quoting attribute values.
-
WriteAttributes
public void WriteAttributes (XmlReader reader, boolDefinitive)
Writes all the attributes of the current node on the specified XmlReader to the XmlWriter.
-
WriteAttributeString
public void WriteAttributeString (string localName, string value)
Writes an attribute.
-
WriteAttributeString
public void WriteAttributeString (string prefix, string localName, string ns, string value)
Writes an attribute with the specified prefix, local name, namespace, and value.
-
WriteAttributeString
public void WriteAttributeString (string localName, string ns, string value)
Writes an attribute with the specified local name, namespace, and value.
-
WriteAttributes
public void WriteAttributes (XmlReader reader, bool defattr)
Writes all attributes of the current node on the specified XmlReader to the XmlWriter.
-
WriteAttributeString
public void WriteAttributeString (string localName, string ns, string value)
Writes an attribute of the specified local name, namespace, and value.
-
WriteAttributeString
public void WriteAttributeString (string prefix, string localName, string ns, string value)
Writes an attribute with the specified prefix, local name, namespace, and value.
-
WriteAttributeString
public void WriteAttributeString (string localName, string value)
Writes an attribute with the specified local name and value.
-
WriteAttributes
public void WriteAttributes (XmlReader reader, bool defattr)
Writes all the attributes of the current node on the specified XmlReader to the XmlWriter.
-
WriteAttributeString
public void WriteAttributeString (string localName, string ns, string value)
Writes an attribute of the specified local name, namespace, and value.
-
WriteAttributeString
public void WriteAttributeString (string prefix, string localName, string ns, string value)
Writes an attribute with the specified prefix, local name, namespace, and value.
-
WriteAttributeString
public void WriteAttributeString (string localName, string value)
Writes an attribute with the specified local name and value.
-
WriteAttributeString
public void WriteAttributeString (string localName, string ns, string value)
Writes an attribute of the specified local name, namespace, and value.
-
WriteAttributeString
public void WriteAttributeString (string prefix, string localName, string ns, string value)
Writes an attribute with the specified prefix, local name, namespace, and value.
-
WriteAttributeString
public void WriteAttributeString (string localName, string value)
Writes an attribute with the specified local name and value.
-
WriteAttributeString
public void WriteAttributeString (string localName, string ns, string value)
Writes an attribute of the specified local name, namespace, and value.
-
WriteAttributeString
public void WriteAttributeString (string prefix, string localName, string ns, string value)
Writes an attribute with the specified prefix, local name, namespace, and value.
-
WriteAttributeString
public void WriteAttributeString (string localName, string value)
Writes an attribute with the specified local name and value.
-
WriteAttributeString
public void WriteAttributeString (string localName, string ns, string value)
Writes an attribute of the specified local name, namespace, and value.
-
WriteAttributeString
public void WriteAttributeString (string prefix, string localName, string ns, string value)
Writes an attribute with the specified prefix, local name, namespace, and value.
Remarks
The XmlWriter
class provides methods to write XML documents or fragments.
It supports various output formats and encodings through the XmlWriterSettings
class.
This class is designed for forward-only, non-cached writing, making it efficient for large XML files.
Example
// Create an XmlWriter that writes to a file. using (XmlWriter writer = XmlWriter.Create("myFile.xml")) { writer.WriteStartDocument(); writer.WriteStartElement("book"); writer.WriteElementString("title", "The Hitchhiker's Guide to the Galaxy"); writer.WriteElementString("author", "Douglas Adams"); writer.WriteEndElement(); // book writer.WriteEndDocument(); }