This documentation is archived and is not being maintained.

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
Assembly:  System.Xml (in System.Xml.dll)

public virtual void WriteNode(
	XmlReader reader,
	bool defattr


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 null.


reader contains invalid characters.

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.

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

using System;
using System.IO;
using System.Xml;

public class Sample{

  public static void Main(){

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

    //Move the reader to the first book element.

    //Create a writer that outputs to the console.
    XmlTextWriter writer = 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.

#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main() 
   XmlTextReader* reader = new XmlTextReader(S"books.xml");
   reader -> WhitespaceHandling = WhitespaceHandling::None;

   // Move the reader to the first book element.
   reader -> MoveToContent();
   reader -> Read();

   // Create a writer that outputs to the console.
   XmlTextWriter* writer = new XmlTextWriter (Console::Out);
   writer -> Formatting = Formatting::Indented;

   // Write the start tag.
   writer -> WriteStartElement(S"myBooks");

   // Write the first book.
   writer -> WriteNode(reader, false);

   // Skip the second book.
   reader -> Skip();

   // Write the last book.
   writer -> WriteNode(reader, false);
   writer -> WriteEndElement();

   // Close the writer and the reader.
   writer -> Close();
   reader -> Close();

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>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0