XmlWriter.WriteNode Method (XPathNavigator, Boolean)
Copies everything from the XPathNavigator object to the writer. The position of the XPathNavigator remains unchanged.
Assembly: System.Xml (in System.Xml.dll)
Parameters
- navigator
- Type: System.Xml.XPath.XPathNavigator
The XPathNavigator to copy from.
- defattr
- Type: System.Boolean
true to copy the default attributes; otherwise, false.
| Exception | Condition |
|---|---|
| ArgumentNullException |
navigator is null. |
The following table shows the supported XPath node types for this method.
|
XPathNodeType |
WriteNode Behavior |
|---|---|
|
Root |
Writes out all the nodes irrespective of type. That is, the writer consumes the XPathNavigator and writes out all the nodes from the root node (including attributes, processing instructions, comments and so on.) |
|
Element |
Writes out the element node and any attribute nodes. |
|
Attribute |
No operation. Use WriteStartAttribute or WriteAttributeString instead. |
|
Text |
Writes out the text node. |
|
Namespace |
No operation. Use the WriteStartAttribute or WriteAttributeString method to write the namespace declaration. |
|
ProcessingInstruction |
Writes out the processing instruction node. |
|
Comment |
Writes out the comment node. |
|
SignificantWhitespace |
Writes out the significant white space node. |
|
Whitespace |
Writes out the white space node. |
The following example uses the WriteNode method to copy the first book node from a document and write it to the console.
using System; using System.IO; using System.Xml; using System.Xml.XPath; public class Sample { public static void Main() { XPathDocument doc = new XPathDocument("books.xml"); XPathNavigator nav = doc.CreateNavigator(); // Create a writer that outputs to the console. XmlWriter writer = XmlWriter.Create(Console.Out); // Write the start tag. writer.WriteStartElement("myBooks"); // Write the first book. nav.MoveToChild("bookstore", ""); nav.MoveToChild("book", ""); writer.WriteNode(nav, false); // Close the start tag. writer.WriteEndElement(); // Close the writer. writer.Close(); } }
The example uses the books.xml file as input.
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
<book genre="autobiography" publicationdate="1981-03-22" 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-11-17" 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-02-15" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.