XmlWriter.Close Metoda

Definice

Při přepsání v odvozené třídě zavře tento datový proud a podkladový datový proud.

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

Výjimky

Volání za účelem zápisu dalšího výstupu po Close zavolání nebo výsledku tohoto volání je neplatný dokument XML.

-nebo-

XmlWriter Před dokončením předchozí asynchronní operace byla volána metoda. V tomto případě InvalidOperationException je vyvolán zpráva "Asynchronní operace již probíhá".

Příklady

Následující příklad zapíše uzel 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

Poznámky

Všechny prvky nebo atributy vlevo otevřené jsou automaticky uzavřeny.

Poznámka

Pokud použijete XmlWriter metody k výstupu XML, elementy a atributy nebudou zapsány, dokud metodu Close nezavoláte. Pokud například používáte XmlWriter k naplnění XmlDocument, dokud nezavřete XmlWriter, nebudete moci sledovat zapsané elementy a atributy v cílovém dokumentu.

Platí pro