Export (0) Print
Expand All

XmlWriter.WriteNode Method (XmlReader, Boolean)

When overridden in a derived class, copies everything from the reader to the writer and moves the reader to the start of the next sibling.

Namespace:  System.Xml
Assemblies:   System.Xml.ReaderWriter (in System.Xml.ReaderWriter.dll)
  System.Xml (in System.Xml.dll)

Public Overridable Sub WriteNode ( _
	reader As XmlReader, _
	defattr As Boolean _


Type: System.Xml.XmlReader

The XmlReader to read from.

Type: System.Boolean

true to copy the default attributes from the XmlReader; otherwise, false.


reader is Nothing.


reader contains invalid characters.


An XmlWriter method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message “An asynchronous operation is already in progress.”

The following table shows the supported node types for this method.


WriteNode Behavior


Writes out all the nodes irrespective of type. That is, the writer consumes the XmlReader and writes out all the nodes read including attributes, processing instructions, comments, and so on.

This situation occurs when the XmlReader is in an initial state. (The XmlReader.ReadState property returns ReaderState.Initial).


Writes out the element node and any attribute nodes.


No operation. Use WriteStartAttribute or WriteAttributeString instead.


Writes out the text node.


Writes out the CDATA section node.


Writes out the entity reference node.


Writes out the processing instruction node.


Writes out the comment node.


Writes out the document type node.


Writes out the significant white space node.


Writes out the white space node.


Writes out the end element tag.


No operation.


Writes out the XML declaration node.

If the reader is in the initial state, this method moves the reader to the end of file. If the reader is already at the end of file or in a closed state, this method is non-operational.

The following C# code copies an entire XML input document to the console:

XmlReader reader = XmlReader.Create(myfile);
XmlWriter writer = XmlWriter.Create(Console.Out);
writer.WriteNode(reader, false);

If you have moved off the root node and are positioned elsewhere in the document the following C# example correctly writes out the nodes.

XmlReader reader = XmlReader.Create(myfile);
reader.Read(); // Read PI
reader.Read(); // Read Comment
reader.Read(); // Read DOCType
XmlWriter writer = XmlWriter.Create(Console.Out);
while (!reader.EOF){
  writer.WriteNode(reader, false);

If the reader is configured to return white space and the writer has is configured to indent output, WriteNode may produce strange output. You will essentially be getting double formatting.

For the asynchronous version of this method, see WriteNodeAsync.

The following example writes the first and last book nodes out to the console.

Imports System
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

    Dim reader as XmlTextReader = new XmlTextReader("books.xml")
    reader.WhitespaceHandling = WhitespaceHandling.None

    'Move the reader to the first book element.

    'Create a writer that outputs to the console. 
    Dim writer as XmlTextWriter = new XmlTextWriter (Console.Out)
    writer.Formatting = Formatting.Indented
    'Write the start tag.

    'Write the first book.
    writer.WriteNode(reader, false)

    'Skip the second book.

    'Write the last book.
    writer.WriteNode(reader, false)

    'Close the writer and the reader.

  end sub
end class

The example uses the file, books.xml, as input.

  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

© 2014 Microsoft