XmlWriter.Close Metoda

Definicja

Gdy przesłonięć w klasie pochodnej, zamyka ten strumień i bazowy strumień.

public:
 virtual void Close();
public:
 abstract void Close();
public virtual void Close ();
public abstract void Close ();
abstract member Close : unit -> unit
override this.Close : unit -> unit
abstract member Close : unit -> unit
Public Overridable Sub Close ()
Public MustOverride Sub Close ()

Wyjątki

Wykonano wywołanie w celu zapisania większej liczby danych wyjściowych po Close wywołaniu lub wynik tego wywołania jest nieprawidłowym dokumentem XML.

-lub-

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

Przykłady

Poniższy przykład zapisuje węzeł XML.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   // Create a writer to write XML to the console.
   XmlWriterSettings^ settings = gcnew XmlWriterSettings;
   settings->Indent = true;
   settings->OmitXmlDeclaration = true;
   XmlWriter^ writer = XmlWriter::Create( Console::Out, settings );
   
   // Write the book element.
   writer->WriteStartElement( L"book" );
   
   // Write the title element.
   writer->WriteStartElement( L"title" );
   writer->WriteString( L"Pride And Prejudice" );
   writer->WriteEndElement();
   
   // Write the close tag for the root element.
   writer->WriteEndElement();
   
   // Write the XML and close the writer.
   writer->Close();
   return 1;
}
using System;
using System.IO;
using System.Xml;

public class Sample {

  public static void Main() {

     // Create a writer to write XML to the console.
     XmlWriterSettings settings = new XmlWriterSettings();
     settings.Indent = true;
     settings.OmitXmlDeclaration = true;
     XmlWriter writer = XmlWriter.Create(Console.Out, settings);

     // Write the book element.
     writer.WriteStartElement("book");

     // Write the title element.
     writer.WriteStartElement("title");
     writer.WriteString("Pride And Prejudice");
     writer.WriteEndElement();

     // Write the close tag for the root element.
     writer.WriteEndElement();

     // Write the XML and close the writer.
     writer.Close();
  }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml

Public Class Sample
    
  Public Shared Sub Main()

     ' Create a writer to write XML to the console.
     Dim settings As XmlWriterSettings = new XmlWriterSettings()
     settings.Indent = true
     settings.OmitXmlDeclaration = true
     Dim writer As XmlWriter = XmlWriter.Create(Console.Out, settings)

     ' Write the book element.
     writer.WriteStartElement("book")
        
     ' Write the title element.
     writer.WriteStartElement("title")
     writer.WriteString("Pride And Prejudice")
     writer.WriteEndElement()
        
     ' Write the close tag for the root element.
     writer.WriteEndElement()
        
     ' Write the XML and close the writer.
     writer.Close()

  End Sub
End Class

Uwagi

Wszystkie otwarte elementy lub atrybuty są automatycznie zamykane.

Uwaga

Jeśli używasz XmlWriter metod do wyprowadzania kodu XML, elementy i atrybuty nie zostaną zapisane do momentu wywołania Close metody . Jeśli na przykład używasz klasy XmlWriter do wypełnienia XmlDocumentobiektu , dopóki nie zamkniesz XmlWriterobiektu , nie będzie można obserwować zapisanych elementów i atrybutów w dokumencie docelowym.

Dotyczy