XmlWriter.WriteEndElement Metode

Definisi

Saat ditimpa dalam kelas turunan, menutup satu elemen dan memunculkan cakupan namespace yang sesuai.

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

Pengecualian

Ini menghasilkan dokumen XML yang tidak valid.

-atau-

Metode XmlWriter dipanggil sebelum operasi asinkron sebelumnya selesai. Dalam hal ini, InvalidOperationException dilemparkan dengan pesan "Operasi asinkron sudah berlangsung."

Contoh

Contoh berikut menggunakan WriteEndElement metode dan WriteFullEndElement .

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;
     XmlWriter writer = XmlWriter.Create(Console.Out, settings);

     // Write the root element.
     writer.WriteStartElement("order");

     // Write an element with attributes.
     writer.WriteStartElement("item");
     writer.WriteAttributeString("date", "2/19/01");
     writer.WriteAttributeString("orderID", "136A5");

     // Write a full end element. Because this element has no
     // content, calling WriteEndElement would have written a
     // short end tag '/>'.
     writer.WriteFullEndElement();

     writer.WriteEndElement();

     // Write the XML to file 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
     Dim writer As XmlWriter = XmlWriter.Create(Console.Out, settings)
        
     ' Write the root element.
     writer.WriteStartElement("order")
        
     ' Write an element with attributes.
     writer.WriteStartElement("item")
     writer.WriteAttributeString("date", "2/19/01")
     writer.WriteAttributeString("orderID", "136A5")
        
     ' Write a full end element. Because this element has no
     ' content, calling WriteEndElement would have written a
     ' short end tag '/>'.
     writer.WriteFullEndElement()
        
     writer.WriteEndElement()

     ' Write the XML to file and close the writer
     writer.Close()

    End Sub
End Class

Keterangan

Jika elemen tidak berisi konten, tag akhir pendek "/>" ditulis; jika tidak, tag akhir lengkap ditulis.

Note

Saat Anda menggunakan XmlWriter metode untuk menghasilkan XML, elemen dan atribut tidak akan ditulis sampai Anda memanggil Close metode . Misalnya, jika Anda menggunakan XmlWriter untuk mengisi XmlDocument, sampai Anda menutup XmlWriter, Anda tidak akan dapat mengamati elemen dan atribut tertulis dalam dokumen target.

Untuk versi asinkron dari metode ini, lihat WriteEndElementAsync.

Berlaku untuk