XmlWriter.WriteNode Metoda

Definicja

Kopiuje wszystko od obiektu źródłowego do bieżącego wystąpienia składnika zapisywania.

Przeciążenia

WriteNode(XmlReader, Boolean)

Po przesłonięciu w klasie pochodnej kopiuje wszystko od czytelnika do pisarza i przenosi czytelnika na początek następnego rodzeństwa.

WriteNode(XPathNavigator, Boolean)

Kopiuje wszystko od XPathNavigator obiektu do modułu zapisywania. Pozycja pozostaje XPathNavigator niezmieniona.

Uwagi

Aby uzyskać asynchroniczną wersję tej metody, zobacz WriteNodeAsync.

WriteNode(XmlReader, Boolean)

Po przesłonięciu w klasie pochodnej kopiuje wszystko od czytelnika do pisarza i przenosi czytelnika na początek następnego rodzeństwa.

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)

Parametry

reader
XmlReader

Element do odczytania XmlReader .

defattr
Boolean

true aby skopiować atrybuty domyślne z elementu XmlReader; w przeciwnym razie false.

Wyjątki

reader to null.

reader zawiera nieprawidłowe znaki.

Metoda XmlWriter została wywołana przed zakończeniem poprzedniej operacji asynchronicznej. W tym przypadku InvalidOperationException jest zgłaszany komunikat "Operacja asynchroniczna jest już w toku".

Przykłady

Poniższy przykład zapisuje pierwsze i ostatnie węzły książki w konsoli programu .

#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

W przykładzie użyto pliku , books.xmljako danych wejściowych.

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

Uwagi

W poniższej tabeli przedstawiono obsługiwane typy węzłów dla tej metody.

Nodetype Zachowanie w węźle writeNode
None Zapisuje wszystkie węzły niezależnie od typu. Oznacza to, że składnik zapisywania używa i XmlReader zapisuje wszystkie węzły odczytane, w tym atrybuty, instrukcje przetwarzania, komentarze itd.

Taka sytuacja występuje, gdy element XmlReader jest w stanie początkowym. (Właściwość XmlReader.ReadState zwraca ReaderState.Initialwartość ).
Element Zapisuje węzeł elementu i wszystkie węzły atrybutów.
Attribute Brak operacji. Użyj WriteStartAttribute polecenia lub WriteAttributeString zamiast tego.
Text Zapisuje węzeł tekstowy.
CDATA Zapisuje węzeł sekcji CDATA.
EntityReference Zapisuje węzeł odwołania do jednostki.
ProcessingInstruction Zapisuje węzeł instrukcji przetwarzania.
Comment Zapisuje węzeł komentarza.
DocumentType Zapisuje węzeł typu dokumentu.
SignificantWhitespace Zapisuje znaczący węzeł odstępu.
Whitespace Zapisuje biały węzeł spacji.
EndElement Zapisuje tag elementu końcowego.
EndEntity Brak operacji.
XmlDeclaration Zapisuje węzeł deklaracji XML.

Jeśli czytnik jest w stanie początkowym, ta metoda przenosi czytnik na koniec pliku. Jeśli czytnik znajduje się już na końcu pliku lub w stanie zamkniętym, ta metoda nie działa.

Następujący kod w języku C# kopiuje cały dokument wejściowy XML do konsoli:

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

Jeśli węzeł główny został przeniesiony i został umieszczony w innym miejscu w dokumencie, poniższy przykład języka C# poprawnie zapisuje węzły.

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

Jeśli czytnik jest skonfigurowany do zwracania białych znaków, a składnik zapisywania jest skonfigurowany do wcięcia danych wyjściowych, WriteNode może spowodować wygenerowanie dziwnych danych wyjściowych. Zasadniczo będziesz otrzymywać podwójne formatowanie.

Aby uzyskać asynchroniczną wersję tej metody, zobacz WriteNodeAsync.

Dotyczy

WriteNode(XPathNavigator, Boolean)

Kopiuje wszystko od XPathNavigator obiektu do modułu zapisywania. Pozycja pozostaje XPathNavigator niezmieniona.

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)

Parametry

navigator
XPathNavigator

Element XPathNavigator do skopiowania.

defattr
Boolean

true aby skopiować atrybuty domyślne; w przeciwnym razie , false.

Wyjątki

navigator to null.

Metoda XmlWriter została wywołana przed zakończeniem poprzedniej operacji asynchronicznej. W tym przypadku InvalidOperationException jest zgłaszany komunikat "Operacja asynchroniczna jest już w toku".

Przykłady

W poniższym przykładzie użyto WriteNode metody do skopiowania pierwszego węzła książki z dokumentu i zapisania go w konsoli programu .

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

W przykładzie użyto pliku books.xml jako danych wejściowych.

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

Uwagi

W poniższej tabeli przedstawiono obsługiwane XPath typy węzłów dla tej metody.

XPathNodeType Zachowanie w węźle writeNode
Root Zapisuje wszystkie węzły niezależnie od typu. Oznacza to, że składnik zapisywania używa i XPathNavigator zapisuje wszystkie węzły z węzła głównego (w tym atrybuty, instrukcje przetwarzania, komentarze itd.).
Element Zapisuje węzeł elementu i wszystkie węzły atrybutów.
Attribute Brak operacji. Użyj WriteStartAttribute polecenia lub WriteAttributeString zamiast tego.
Text Zapisuje węzeł tekstowy.
Namespace Brak operacji. WriteStartAttribute Użyj metody orWriteAttributeString, aby zapisać deklarację przestrzeni nazw.
ProcessingInstruction Zapisuje węzeł instrukcji przetwarzania.
Comment Zapisuje węzeł komentarza.
SignificantWhitespace Zapisuje znaczący węzeł odstępu.
Whitespace Zapisuje biały węzeł spacji.

Aby uzyskać asynchroniczną wersję tej metody, zobacz WriteNodeAsync.

Dotyczy