XmlWriter.WriteNode XmlWriter.WriteNode XmlWriter.WriteNode XmlWriter.WriteNode Method

定義

將來源物件中的每個項目複製到目前的寫入器執行個體。Copies everything from the source object to the current writer instance.

多載

WriteNode(XmlReader, Boolean) WriteNode(XmlReader, Boolean) WriteNode(XmlReader, Boolean) WriteNode(XmlReader, Boolean)

在衍生類別中覆寫時,從讀取器複製所有內容至寫入器,並將讀取器移至下一個同層級 (Sibling) 的開頭。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) WriteNode(XPathNavigator, Boolean) WriteNode(XPathNavigator, Boolean) WriteNode(XPathNavigator, Boolean)

XPathNavigator 物件中的所有項目複製到寫入器。Copies everything from the XPathNavigator object to the writer. XPathNavigator 的位置保持不變。The position of the XPathNavigator remains unchanged.

備註

這個方法的非同步版本,請參閱WriteNodeAsyncFor the asynchronous version of this method, see WriteNodeAsync.

WriteNode(XmlReader, Boolean) WriteNode(XmlReader, Boolean) WriteNode(XmlReader, Boolean) WriteNode(XmlReader, Boolean)

在衍生類別中覆寫時,從讀取器複製所有內容至寫入器,並將讀取器移至下一個同層級 (Sibling) 的開頭。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)

參數

reader
XmlReader XmlReader XmlReader XmlReader

要從其中讀取的 XmlReaderThe XmlReader to read from.

defattr
Boolean Boolean Boolean Boolean

若要從 true 複製預設屬性,則為 XmlReader,否則為 falsetrue to copy the default attributes from the XmlReader; otherwise, false.

例外狀況

reader 包含無效的字元。reader contains invalid characters.

在先前的非同步作業完成前呼叫了 XmlWriter 方法。An XmlWriter method was called before a previous asynchronous operation finished. 在此情況下,會擲回 InvalidOperationException 與「非同步作業已經在進行中」的訊息。In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

範例

下列範例會寫出到主控台的第一個和最後一個書籍節點。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
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

此範例會使用該檔案,books.xml作為輸入。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>

備註

下表顯示支援的節點型別,這個方法。The following table shows the supported node types for this method.

NodeTypeNodeType WriteNode BehaviorWriteNode Behavior
None 寫出不論類型為何的所有節點。Writes out all the nodes irrespective of type. 也就是寫入器會取用XmlReader並寫出所有節點讀取,包括屬性、 處理指示、 註解等等。That is, the writer consumes the XmlReader and writes out all the nodes read including attributes, processing instructions, comments, and so on.

發生這種情況時XmlReader處於初始狀態。This situation occurs when the XmlReader is in an initial state. (XmlReader.ReadState屬性會傳回ReaderState.Initial)。(The XmlReader.ReadState property returns ReaderState.Initial).
Element 寫出項目節點和 any 屬性節點。Writes out the element node and any attribute nodes.
Attribute 無作業。No operation. 請改用 WriteStartAttributeWriteAttributeStringUse WriteStartAttribute or WriteAttributeString instead.
Text 寫出文字節點。Writes out the text node.
CDATA 寫出 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 寫出 XML 宣告節點。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.

下列 C# 程式碼會將整個 XML 輸入文件複製到主控台: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);  

如果您已關閉的根節點,並在其他地方放置在正確,下列 C# 範例會寫出該節點的文件。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);  
 }  

如果設定讀取器傳回泛空白字元和寫入器已設定為縮排輸出,WriteNode可能會產生很奇怪的輸出。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.

這個方法的非同步版本,請參閱WriteNodeAsyncFor the asynchronous version of this method, see WriteNodeAsync.

WriteNode(XPathNavigator, Boolean) WriteNode(XPathNavigator, Boolean) WriteNode(XPathNavigator, Boolean) WriteNode(XPathNavigator, Boolean)

XPathNavigator 物件中的所有項目複製到寫入器。Copies everything from the XPathNavigator object to the writer. XPathNavigator 的位置保持不變。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)

參數

navigator
XPathNavigator XPathNavigator XPathNavigator XPathNavigator

要複製的來源 XPathNavigatorThe XPathNavigator to copy from.

defattr
Boolean Boolean Boolean Boolean

true 表示複製預設屬性,否則為 falsetrue to copy the default attributes; otherwise, false.

例外狀況

在先前的非同步作業完成前呼叫了 XmlWriter 方法。An XmlWriter method was called before a previous asynchronous operation finished. 在此情況下,會擲回 InvalidOperationException 與「非同步作業已經在進行中」的訊息。In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

範例

下列範例會使用WriteNode方法來複製文件中的第一個書籍節點,並寫入主控台。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
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

此範例會使用 books.xml 檔案做為輸入。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>

備註

下表顯示支援XPath節點型別,這個方法。The following table shows the supported XPath node types for this method.

XPathNodeTypeXPathNodeType WriteNode BehaviorWriteNode Behavior
Root 寫出不論類型為何的所有節點。Writes out all the nodes irrespective of type. 也就是寫入器會取用XPathNavigator並寫出所有節點中,從根節點 (包括屬性、 處理指示、 註解等等)。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 寫出項目節點和 any 屬性節點。Writes out the element node and any attribute nodes.
Attribute 無作業。No operation. 請改用 WriteStartAttributeWriteAttributeStringUse WriteStartAttribute or WriteAttributeString instead.
Text 寫出文字節點。Writes out the text node.
Namespace 無作業。No operation. 使用WriteStartAttributeWriteAttributeString来撰寫命名空間宣告的方法。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.

這個方法的非同步版本,請參閱WriteNodeAsyncFor the asynchronous version of this method, see WriteNodeAsync.

適用於