Windows Dev Center

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)

public abstract class XmlWriter : IDisposable

The XmlWriter type exposes the following members.

  NameDescription
Protected methodXmlWriterInitializes a new instance of the XmlWriter class.
Top

  NameDescription
Public propertySettingsGets the XmlWriterSettings object used to create this XmlWriter instance.
Public propertyWriteStateWhen overridden in a derived class, gets the state of the writer.
Public propertyXmlLangWhen overridden in a derived class, gets the current xml:lang scope.
Public propertyXmlSpaceWhen overridden in a derived class, gets an XmlSpace representing the current xml:space scope.
Top

  NameDescription
Public methodCloseWhen overridden in a derived class, closes this stream and the underlying stream.
Public methodStatic memberCreate(Stream)Creates a new XmlWriter instance using the specified stream.
Public methodStatic memberCreate(StringBuilder)Creates a new XmlWriter instance using the specified StringBuilder.
Public methodStatic memberCreate(TextWriter)Creates a new XmlWriter instance using the specified TextWriter.
Public methodStatic memberCreate(XmlWriter)Creates a new XmlWriter instance using the specified XmlWriter object.
Public methodStatic memberCreate(Stream, XmlWriterSettings)Creates a new XmlWriter instance using the stream and XmlWriterSettings object.
Public methodStatic memberCreate(StringBuilder, XmlWriterSettings)Creates a new XmlWriter instance using the StringBuilder and XmlWriterSettings objects.
Public methodStatic memberCreate(TextWriter, XmlWriterSettings)Creates a new XmlWriter instance using the TextWriter and XmlWriterSettings objects.
Public methodStatic memberCreate(XmlWriter, XmlWriterSettings)Creates a new XmlWriter instance using the specified XmlWriter and XmlWriterSettings objects.
Public methodDispose()Releases all resources used by the current instance of the XmlWriter class.
Protected methodDispose(Boolean)Releases the unmanaged resources used by the XmlWriter and optionally releases the managed resources.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodFlushWhen overridden in a derived class, flushes whatever is in the buffer to the underlying streams and also flushes the underlying stream.
Public methodFlushAsyncAsynchronously flushes whatever is in the buffer to the underlying streams and also flushes the underlying stream.
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodLookupPrefixWhen overridden in a derived class, returns the closest prefix defined in the current namespace scope for the namespace URI.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodWriteAttributesWhen overridden in a derived class, writes out all the attributes found at the current position in the XmlReader.
Public methodWriteAttributesAsyncAsynchronously writes out all the attributes found at the current position in the XmlReader.
Public methodWriteAttributeString(String, String)When overridden in a derived class, writes out the attribute with the specified local name and value.
Public methodWriteAttributeString(String, String, String)When overridden in a derived class, writes an attribute with the specified local name, namespace URI, and value.
Public methodWriteAttributeString(String, String, String, String)When overridden in a derived class, writes out the attribute with the specified prefix, local name, namespace URI, and value.
Public methodWriteAttributeStringAsyncAsynchronously writes out the attribute with the specified prefix, local name, namespace URI, and value.
Public methodWriteBase64When overridden in a derived class, encodes the specified binary bytes as Base64 and writes out the resulting text.
Public methodWriteBase64AsyncAsynchronously encodes the specified binary bytes as Base64 and writes out the resulting text.
Public methodWriteBinHexWhen overridden in a derived class, encodes the specified binary bytes as BinHex and writes out the resulting text.
Public methodWriteBinHexAsyncAsynchronously encodes the specified binary bytes as BinHex and writes out the resulting text.
Public methodWriteCDataWhen overridden in a derived class, writes out a <![CDATA[...]]> block containing the specified text.
Public methodWriteCDataAsyncAsynchronously writes out a <![CDATA[...]]> block containing the specified text.
Public methodWriteCharEntityWhen overridden in a derived class, forces the generation of a character entity for the specified Unicode character value.
Public methodWriteCharEntityAsyncAsynchronously forces the generation of a character entity for the specified Unicode character value.
Public methodWriteCharsWhen overridden in a derived class, writes text one buffer at a time.
Public methodWriteCharsAsyncAsynchronously writes text one buffer at a time.
Public methodWriteCommentWhen overridden in a derived class, writes out a comment <!--...--> containing the specified text.
Public methodWriteCommentAsyncAsynchronously writes out a comment <!--...--> containing the specified text.
Public methodWriteDocTypeWhen overridden in a derived class, writes the DOCTYPE declaration with the specified name and optional attributes.
Public methodWriteDocTypeAsyncAsynchronously writes the DOCTYPE declaration with the specified name and optional attributes.
Public methodWriteElementString(String, String)When overridden in a derived class, writes an element with the specified local name and value.
Public methodWriteElementString(String, String, String)When overridden in a derived class, writes an element with the specified local name, namespace URI, and value.
Public methodWriteElementString(String, String, String, String)Writes an element with the specified local name, namespace URI, and value.
Public methodWriteElementStringAsyncAsynchronously writes an element with the specified prefix, local name, namespace URI, and value.
Public methodWriteEndAttributeWhen overridden in a derived class, closes the previous WriteStartAttribute call.
Protected methodWriteEndAttributeAsyncAsynchronously closes the previous WriteStartAttribute call.
Public methodWriteEndDocumentWhen overridden in a derived class, closes any open elements or attributes and puts the writer back in the Start state.
Public methodWriteEndDocumentAsyncAsynchronously closes any open elements or attributes and puts the writer back in the Start state.
Public methodWriteEndElementWhen overridden in a derived class, closes one element and pops the corresponding namespace scope.
Public methodWriteEndElementAsyncAsynchronously closes one element and pops the corresponding namespace scope.
Public methodWriteEntityRefWhen overridden in a derived class, writes out an entity reference as &name;.
Public methodWriteEntityRefAsyncAsynchronously writes out an entity reference as &name;.
Public methodWriteFullEndElementWhen overridden in a derived class, closes one element and pops the corresponding namespace scope.
Public methodWriteFullEndElementAsyncAsynchronously closes one element and pops the corresponding namespace scope.
Public methodWriteNameWhen overridden in a derived class, writes out the specified name, ensuring it is a valid name according to the W3C XML 1.0 recommendation (http://www.w3.org/TR/1998/REC-xml-19980210#NT-Name).
Public methodWriteNameAsyncAsynchronously writes out the specified name, ensuring it is a valid name according to the W3C XML 1.0 recommendation (http://www.w3.org/TR/1998/REC-xml-19980210#NT-Name).
Public methodWriteNmTokenWhen overridden in a derived class, writes out the specified name, ensuring it is a valid NmToken according to the W3C XML 1.0 recommendation (http://www.w3.org/TR/1998/REC-xml-19980210#NT-Name).
Public methodWriteNmTokenAsyncAsynchronously writes out the specified name, ensuring it is a valid NmToken according to the W3C XML 1.0 recommendation (http://www.w3.org/TR/1998/REC-xml-19980210#NT-Name).
Public methodWriteNodeWhen overridden in a derived class, copies everything from the reader to the writer and moves the reader to the start of the next sibling.
Public methodWriteNodeAsyncAsynchronously copies everything from the reader to the writer and moves the reader to the start of the next sibling.
Public methodWriteProcessingInstructionWhen overridden in a derived class, writes out a processing instruction with a space between the name and text as follows: <?name text?>.
Public methodWriteProcessingInstructionAsyncAsynchronously writes out a processing instruction with a space between the name and text as follows: <?name text?>.
Public methodWriteQualifiedNameWhen overridden in a derived class, writes out the namespace-qualified name. This method looks up the prefix that is in scope for the given namespace.
Public methodWriteQualifiedNameAsyncAsynchronously writes out the namespace-qualified name. This method looks up the prefix that is in scope for the given namespace.
Public methodWriteRaw(String)When overridden in a derived class, writes raw markup manually from a string.
Public methodWriteRaw(Char[], Int32, Int32)When overridden in a derived class, writes raw markup manually from a character buffer.
Public methodWriteRawAsync(String)Asynchronously writes raw markup manually from a string.
Public methodWriteRawAsync(Char[], Int32, Int32)Asynchronously writes raw markup manually from a character buffer.
Public methodWriteStartAttribute(String)Writes the start of an attribute with the specified local name.
Public methodWriteStartAttribute(String, String)Writes the start of an attribute with the specified local name and namespace URI.
Public methodWriteStartAttribute(String, String, String)When overridden in a derived class, writes the start of an attribute with the specified prefix, local name, and namespace URI.
Protected methodWriteStartAttributeAsyncAsynchronously writes the start of an attribute with the specified prefix, local name, and namespace URI.
Public methodWriteStartDocument()When overridden in a derived class, writes the XML declaration with the version "1.0".
Public methodWriteStartDocument(Boolean)When overridden in a derived class, writes the XML declaration with the version "1.0" and the standalone attribute.
Public methodWriteStartDocumentAsync()Asynchronously writes the XML declaration with the version "1.0".
Public methodWriteStartDocumentAsync(Boolean)Asynchronously writes the XML declaration with the version "1.0" and the standalone attribute.
Public methodWriteStartElement(String)When overridden in a derived class, writes out a start tag with the specified local name.
Public methodWriteStartElement(String, String)When overridden in a derived class, writes the specified start tag and associates it with the given namespace.
Public methodWriteStartElement(String, String, String)When overridden in a derived class, writes the specified start tag and associates it with the given namespace and prefix.
Public methodWriteStartElementAsyncAsynchronously writes the specified start tag and associates it with the given namespace and prefix.
Public methodWriteStringWhen overridden in a derived class, writes the given text content.
Public methodWriteStringAsyncAsynchronously writes the given text content.
Public methodWriteSurrogateCharEntityWhen overridden in a derived class, generates and writes the surrogate character entity for the surrogate character pair.
Public methodWriteSurrogateCharEntityAsyncAsynchronously generates and writes the surrogate character entity for the surrogate character pair.
Public methodWriteValue(Boolean)Writes a Boolean value.
Public methodWriteValue(DateTime)Writes a DateTime value.
Public methodWriteValue(DateTimeOffset)Writes a DateTimeOffset value.
Public methodWriteValue(Decimal)Writes a Decimal value.
Public methodWriteValue(Double)Writes a Double value.
Public methodWriteValue(Int32)Writes a Int32 value.
Public methodWriteValue(Int64)Writes a Int64 value.
Public methodWriteValue(Object)Writes the object value.
Public methodWriteValue(Single)Writes a single-precision floating-point number.
Public methodWriteValue(String)Writes a String value.
Public methodWriteWhitespaceWhen overridden in a derived class, writes out the given white space.
Public methodWriteWhitespaceAsyncAsynchronously writes out the given white space.
Top

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

NoteNote:

You must use the Create method to create XmlWriter object.

Note   When you use the XmlWriter methods to output XML, the elements and attributes will not be written until you call the Close method.

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.

The following example navigates through the stream to determine the current node type, and then uses XmlWriter to output the XmlReader content.


StringBuilder output = new StringBuilder();

String xmlString =
        @"<?xml version='1.0'?>
        <!-- This is a sample XML document -->
        <Items>
          <Item>test with a child element <more/> stuff</Item>
        </Items>";
// Create an XmlReader
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
    XmlWriterSettings ws = new XmlWriterSettings();
    ws.Indent = true;
    using (XmlWriter writer = XmlWriter.Create(output, ws))
    {

        // Parse the file and display each of the nodes.
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:
                    writer.WriteStartElement(reader.Name);
                    break;
                case XmlNodeType.Text:
                    writer.WriteString(reader.Value);
                    break;
                case XmlNodeType.XmlDeclaration:
                case XmlNodeType.ProcessingInstruction:
                    writer.WriteProcessingInstruction(reader.Name, reader.Value);
                    break;
                case XmlNodeType.Comment:
                    writer.WriteComment(reader.Value);
                    break;
                case XmlNodeType.EndElement:
                    writer.WriteFullEndElement();
                    break;
            }
        }

    }
}
OutputTextBlock.Text = output.ToString();


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

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

Show:
© 2015 Microsoft