แก้ไข

แชร์ผ่าน


XmlWriter.WriteNode Method

Definition

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

Overloads

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

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.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ reader = gcnew 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 = gcnew 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();
}
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 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.

For the asynchronous version of this method, see WriteNodeAsync.

Applies to

WriteNode(XPathNavigator, Boolean)

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

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

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

Parameters

navigator
XPathNavigator

The XPathNavigator to copy from.

defattr
Boolean

true to copy the default attributes; otherwise, false.

Exceptions

navigator is null.

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 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();
    }
}
Imports System.IO
Imports System.Xml
Imports System.Xml.XPath

Module Module1

    Sub Main()

        Dim doc As XPathDocument = New XPathDocument("books.xml")
        Dim nav As XPathNavigator = doc.CreateNavigator()

        ' Create a writer that outputs to the console.
        Dim writer As XmlWriter = 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()

    End Sub
End Module

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>

Remarks

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.

For the asynchronous version of this method, see WriteNodeAsync.

Applies to