XmlWriter.WriteNode 方法

定義

將來源物件中的每個項目複製到目前的寫入器執行個體。

多載

WriteNode(XmlReader, Boolean)

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

WriteNode(XPathNavigator, Boolean)

XPathNavigator 物件中的所有項目複製到寫入器。 XPathNavigator 的位置保持不變。

備註

如需這個方法的非同步版本,請參閱 WriteNodeAsync

WriteNode(XmlReader, Boolean)

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

defattr
Boolean

若要從 true 複製預設屬性,則為 XmlReader,否則為 false

例外狀況

readernull

reader 包含無效的字元。

在先前的非同步作業完成前呼叫了 XmlWriter 方法。 在此情況下,會擲回 InvalidOperationException 與「非同步作業已經在進行中」的訊息。

範例

下列範例會將第一個和最後一個書籍節點寫入主控台。

#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

此範例會使用 檔案 books.xml ,作為輸入。

<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>

備註

下表顯示此方法支援的節點類型。

NodeType WriteNode 行為
None 不論類型為何,都會寫出所有節點。 也就是說,寫入器會 XmlReader 取用 並寫出讀取的所有節點,包括屬性、處理指示、批註等等。

當 處於初始狀態時 XmlReader ,就會發生這種情況。 (屬性 XmlReader.ReadState 會傳 ReaderState.Initial 回) 。
Element 寫出專案節點和任何屬性節點。
Attribute 無作業。 請改用 WriteStartAttributeWriteAttributeString
Text 寫出文字節點。
CDATA 寫出 CDATA 區段節點。
EntityReference 寫出實體參考節點。
ProcessingInstruction 寫出處理指示節點。
Comment 寫出批註節點。
DocumentType 寫出檔案類型節點。
SignificantWhitespace 寫出重要的空白字元節點。
Whitespace 寫出空白字元節點。
EndElement 寫出結束專案標記。
EndEntity 無作業。
XmlDeclaration 寫出 XML 宣告節點。

如果讀取器處於初始狀態,這個方法會將讀取器移至檔案結尾。 如果讀取器已經在檔案結尾或處於關閉狀態,則這個方法無法運作。

下列 C# 程式碼會將整個 XML 輸入檔案複製到主控台:

XmlReader reader = XmlReader.Create(myfile);
XmlWriter writer = XmlWriter.Create(Console.Out);
writer.WriteNode(reader, false);

如果您已移離根節點,並位於檔中的其他位置,下列 C# 範例會正確地寫出節點。

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 可能會產生奇怪的輸出。 您基本上會取得雙重格式設定。

如需這個方法的非同步版本,請參閱 WriteNodeAsync

適用於

WriteNode(XPathNavigator, Boolean)

XPathNavigator 物件中的所有項目複製到寫入器。 XPathNavigator 的位置保持不變。

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

defattr
Boolean

true 表示複製預設屬性,否則為 false

例外狀況

navigatornull

在先前的非同步作業完成前呼叫了 XmlWriter 方法。 在此情況下,會擲回 InvalidOperationException 與「非同步作業已經在進行中」的訊息。

範例

下列範例會 WriteNode 使用 方法來從檔案複製第一個書籍節點,並將其寫入主控台。

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

此範例會使用 books.xml 檔案作為輸入。

<?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 節點類型。

XPathNodeType WriteNode 行為
Root 不論類型為何,都會寫出所有節點。 也就是說,寫入器會 XPathNavigator 取用 ,並從根節點寫出所有節點, (包括屬性、處理指示、批註等等。)
Element 寫出專案節點和任何屬性節點。
Attribute 無作業。 請改用 WriteStartAttributeWriteAttributeString
Text 寫出文字節點。
Namespace 無作業。 WriteStartAttribute使用 或 WriteAttributeString 方法來寫入命名空間宣告。
ProcessingInstruction 寫出處理指示節點。
Comment 寫出批註節點。
SignificantWhitespace 寫出重要的空白字元節點。
Whitespace 寫出空白字元節點。

如需這個方法的非同步版本,請參閱 WriteNodeAsync

適用於