XmlWriter.WriteEndElement Metoda

Definicja

Po przesłonięciu w klasie pochodnej zamyka jeden element i wyświetla odpowiadający mu zakres przestrzeni nazw.

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

Wyjątki

Powoduje to nieprawidłowy dokument 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

W poniższym przykładzie użyto WriteEndElement metod i 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

Uwagi

Jeśli element nie zawiera zawartości, zostanie napisany krótki tag końcowy "/>"; w przeciwnym razie zostanie zapisany pełny tag końcowy.

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.

Aby uzyskać asynchroniczną wersję tej metody, zobacz WriteEndElementAsync.

Dotyczy