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.
- reader
- The System.Xml.XmlReader to read from.
- defattr
- true to copy the default attributes from the XmlReader; otherwise, false.
Type Reason ArgumentNullException reader is null. InvalidOperationException The XmlWriter.WriteState is WriteState.Closed.
The following table shows the supported node types for this method.
None |
Writes out all the nodes irrespective of type. That is, the writer consumes the System.Xml.XmlReader and writes out all the nodes read including attributes, processing instructions, comments, and so on. This situation occurs when the System.Xml.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 XmlWriter.WriteStartAttribute(string, string) or XmlWriter.WriteAttributeString(string, string, string) 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:
Example
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.
Example
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 XmlWriter.WriteNodeAsync(XmlReader, bool).
The following example uses a System.Xml.XmlTextReader and a System.Xml.XmlTextWriter to copy an XML file, specified in the command line, to the console.
C# Example
using System; using System.Xml; public class Copier { public static void Main(string[] args) { XmlTextReader xtReader = new XmlTextReader(args[0]); XmlTextWriter xtWriter = new XmlTextWriter(Console.Out); xtWriter.WriteNode(xtReader, false); xtWriter.Close(); xtReader.Close(); } }