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

public virtual void WriteNode (
	XmlReader reader,
	bool defattr
)
public void WriteNode (
	XmlReader reader, 
	boolean defattr
)
public function WriteNode (
	reader : XmlReader, 
	defattr : boolean
)

Parameters

reader

The XmlReader to read from.

defattr

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

Exception typeCondition

ArgumentNullException

reader is a null reference (Nothing in Visual Basic).

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 WriteAttributeString instead.

Text

Writes out the text node.

CDATA

Writes out the CDATA section node.

EntityReference

Writes out the entity reference node.

ProcessingInstruction

Writes out the processing instruction node.

Comment

Writes out the comment node.

DocumentType

Writes out the document type node.

SignificantWhitespace

Writes out the significant white space node.

Whitespace

Writes out the white space node.

EndElement

Writes out the end element tag.

EndEntity

No operation.

XmlDeclaration

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.
    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();

  }
}

import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        XmlTextReader reader = new XmlTextReader("books.xml");
        reader.set_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.get_Out());
        writer.set_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();
    } //main 
} //Sample

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>

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft