XmlWriter.Close Método

Definição

Quando substituído em uma classe derivada, fecha esse fluxo e o fluxo subjacente.

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

Exceções

Uma chamada é feita para gravar mais saída depois Close de ter sido chamada ou o resultado dessa chamada é um documento XML inválido.

- ou -

Um XmlWriter método foi chamado antes de uma operação assíncrona anterior ser concluída. Nesse caso, InvalidOperationException é lançada com a mensagem "Uma operação assíncrona já está em andamento".

Exemplos

O exemplo a seguir grava um nó XML.

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

Comentários

Todos os elementos ou atributos deixados abertos são fechados automaticamente.

Note

Quando você usa os XmlWriter métodos para gerar XML, os elementos e atributos não serão gravados até que você chame o Close método. Por exemplo, se você estiver usando o XmlWriter para preencher um XmlDocument, até fechar, XmlWriternão será possível observar os elementos e atributos escritos no documento de destino.

Aplica-se a