XmlWriter.WriteNode Method

Definition

Copies everything from the source object to the current writer instance.

Overloads

Name Description
WriteNode(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.

WriteNode(XPathNavigator, Boolean)

Copies everything from the XPathNavigator object to the writer. The position of the XPathNavigator remains unchanged.

Remarks

For the asynchronous version of this method, see WriteNodeAsync.

WriteNode(XmlReader, Boolean)

Source:
XmlWriter.cs
Source:
XmlWriter.cs
Source:
XmlWriter.cs
Source:
XmlWriter.cs
Source:
XmlWriter.cs

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.

public:
 virtual void WriteNode(System::Xml::XmlReader ^ reader, bool defattr);
public virtual void WriteNode(System.Xml.XmlReader reader, bool defattr);
abstract member WriteNode : System.Xml.XmlReader * bool -> unit
override this.WriteNode : System.Xml.XmlReader * bool -> unit
Public Overridable Sub WriteNode (reader As XmlReader, defattr As Boolean)

Parameters

reader
XmlReader

The XmlReader to read from.

defattr
Boolean

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

Exceptions

reader is null.

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

Examples

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.
    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("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();
  }
}
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.
    reader.MoveToContent()
    reader.Read()

    'Create a writer that outputs to the console.
    Dim writer as XmlTextWriter = new XmlTextWriter (Console.Out)
    writer.Formatting = Formatting.Indented
    
    'Write the start tag.
    writer.WriteStartElement("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()

  end sub
end class

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

<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

Remarks

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

NodeType WriteNode Behavior
None 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).
Element Writes out the element node and any attribute nodes.
Attribute No operation. Use WriteStartAttribute or