Bagikan melalui


XmlWriter.WriteNode Metode

Definisi

Menyalin semuanya dari objek sumber ke instans penulis saat ini.

Overload

Nama Deskripsi
WriteNode(XmlReader, Boolean)

Ketika ditimpa di kelas turunan, salin semuanya dari pembaca ke penulis dan memindahkan pembaca ke awal saudara kandung berikutnya.

WriteNode(XPathNavigator, Boolean)

Menyalin semuanya dari XPathNavigator objek ke penulis. Posisi tetap XPathNavigator tidak berubah.

Keterangan

Untuk versi asinkron dari metode ini, lihat WriteNodeAsync.

WriteNode(XmlReader, Boolean)

Sumber:
XmlWriter.cs
Sumber:
XmlWriter.cs
Sumber:
XmlWriter.cs
Sumber:
XmlWriter.cs
Sumber:
XmlWriter.cs

Ketika ditimpa di kelas turunan, salin semuanya dari pembaca ke penulis dan memindahkan pembaca ke awal saudara kandung berikutnya.

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)

Parameter

reader
XmlReader

Untuk XmlReader membaca dari.

defattr
Boolean

true untuk menyalin atribut default dari XmlReader; jika tidak, false.

Pengecualian

reader adalah null.

reader berisi karakter yang tidak valid.

Metode XmlWriter dipanggil sebelum operasi asinkron sebelumnya selesai. Dalam hal ini, InvalidOperationException dilemparkan dengan pesan "Operasi asinkron sudah berlangsung."

Contoh

Contoh berikut menulis node buku pertama dan terakhir ke konsol.

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

Contohnya menggunakan file, books.xml, sebagai 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>

Keterangan

Tabel berikut ini memperlihatkan jenis simpul yang didukung untuk metode ini.

NodeType Perilaku WriteNode
None Menulis semua simpul terlepas dari jenisnya. Artinya, penulis mengonsumsi XmlReader dan menulis semua simpul yang dibaca termasuk atribut, instruksi pemrosesan, komentar, dan sebagainya.

Situasi ini terjadi ketika XmlReader berada dalam keadaan awal. (Properti XmlReader.ReadState mengembalikan ReaderState.Initial).
Element Menulis node elemen dan node atribut apa pun.
Attribute Tidak ada operasi. Gunakan WriteStartAttribute atau WriteAttributeString sebagai gantinya.
Text Menuliskan simpul teks.
CDATA Menulis node bagian CDATA.
EntityReference Menuliskan simpul referensi entitas.
ProcessingInstruction Menulis node instruksi pemrosesan.
Comment Menuliskan simpul komentar.
DocumentType Menuliskan node jenis dokumen.
SignificantWhitespace Menuliskan simpul ruang putih yang signifikan.
Whitespace Menuliskan simpul spasi putih.
EndElement Menuliskan tag elemen akhir.
EndEntity Tidak ada operasi.
XmlDeclaration Menulis simpul deklarasi XML.

Jika pembaca dalam status awal, metode ini memindahkan pembaca ke akhir file. Jika pembaca sudah berada di akhir file atau dalam keadaan tertutup, metode ini tidak beroperasi.

Kode C# berikut menyalin seluruh dokumen input XML ke konsol:

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

Jika Anda telah memindahkan simpul akar dan diposisikan di tempat lain dalam dokumen, contoh C# berikut menulis simpul dengan benar.

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);
 }

Jika pembaca dikonfigurasi untuk mengembalikan spasi kosong dan penulis telah dikonfigurasi ke output inden, WriteNode dapat menghasilkan output yang aneh. Anda pada dasarnya akan mendapatkan pemformatan ganda.

Untuk versi asinkron dari metode ini, lihat WriteNodeAsync.

Berlaku untuk

WriteNode(XPathNavigator, Boolean)

Sumber:
XmlWriter.cs
Sumber:
XmlWriter.cs
Sumber:
XmlWriter.cs
Sumber:
XmlWriter.cs
Sumber:
XmlWriter.cs

Menyalin semuanya dari XPathNavigator objek ke penulis. Posisi tetap XPathNavigator tidak berubah.

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)

Parameter

navigator
XPathNavigator

yang XPathNavigator akan disalin dari.

defattr
Boolean

true untuk menyalin atribut default; jika tidak, false.

Pengecualian

navigator adalah null.

Metode XmlWriter dipanggil sebelum operasi asinkron sebelumnya selesai. Dalam hal ini, InvalidOperationException dilemparkan dengan pesan "Operasi asinkron sudah berlangsung."

Contoh

Contoh berikut menggunakan WriteNode metode untuk menyalin simpul buku pertama dari dokumen dan menulisnya ke konsol.

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

Contoh menggunakan file books.xml sebagai 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>

Keterangan

Tabel berikut ini memperlihatkan jenis simpul yang didukung XPath untuk metode ini.

XPathNodeType Perilaku WriteNode
Root Menulis semua simpul terlepas dari jenisnya. Artinya, penulis mengonsumsi XPathNavigator dan menulis semua simpul dari simpul akar (termasuk atribut, instruksi pemrosesan, komentar dan sebagainya.)
Element Menulis node elemen dan node atribut apa pun.
Attribute Tidak ada operasi. Gunakan WriteStartAttribute atau WriteAttributeString sebagai gantinya.
Text Menuliskan simpul teks.
Namespace Tidak ada operasi. WriteStartAttribute Gunakan metode atau WriteAttributeString untuk menulis deklarasi namespace layanan.
ProcessingInstruction Menulis node instruksi pemrosesan.
Comment Menuliskan simpul komentar.
SignificantWhitespace Menuliskan simpul ruang putih yang signifikan.
Whitespace Menuliskan simpul spasi putih.

Untuk versi asinkron dari metode ini, lihat WriteNodeAsync.

Berlaku untuk