XmlWriter.Close XmlWriter.Close XmlWriter.Close XmlWriter.Close Method

Definición

Cuando se invalida en una clase derivada, cierra este flujo y el flujo subyacente.When overridden in a derived class, closes this stream and the underlying stream.

public:
 abstract void Close();
public abstract void Close ();
abstract member Close : unit -> unit
Public MustOverride Sub Close ()

Excepciones

Después de llamar a Close o si el resultado de dicha llamada no es un documento XML válido, se realiza una llamada para escribir más resultados.A call is made to write more output after Close has been called or the result of this call is an invalid XML document.

o bien-or-

Se llamó un método XmlWriter antes de que se termine una operación asincrónica anterior.An XmlWriter method was called before a previous asynchronous operation finished. En este caso, se genera InvalidOperationException con el mensaje “Ya hay una operación asincrónica en curso”.In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

Ejemplos

En el ejemplo siguiente se escribe un nodo XML.The following example writes an XML node.

#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 'Sample

Comentarios

Los elementos o atributos abiertos se cierran automáticamente.Any elements or attributes left open are automatically closed.

Nota

Cuando se usan los XmlWriter métodos para generar XML, los elementos y atributos no se escribirán hasta que se llame Close al método.When you use the XmlWriter methods to output XML, the elements and attributes will not be written until you call the Close method. Por ejemplo, si usa XmlWriter para rellenar un XmlDocument, hasta que XmlWritercierre, no podrá observar los elementos y atributos escritos en el documento de destino.For example, if you are using the XmlWriter to populate an XmlDocument, until you close the XmlWriter, you will not be able to observe the written elements and attributes in the target document.

Se aplica a