XmlWriter.WriteEndElement XmlWriter.WriteEndElement XmlWriter.WriteEndElement XmlWriter.WriteEndElement Method

定義

在衍生類別中覆寫時,關閉一個項目並取出對應的命名空間範圍。When overridden in a derived class, closes one element and pops the corresponding namespace scope.

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

例外狀況

這會導致無效的 XML 文件。This results in an invalid XML document.

-或--or-

在先前的非同步作業完成前呼叫了 XmlWriter 方法。An XmlWriter method was called before a previous asynchronous operation finished. 在此情況下,會擲回 InvalidOperationException 與「非同步作業已經在進行中」的訊息。In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

範例

下列範例會使用WriteEndElementWriteFullEndElement方法。The following example uses the WriteEndElement and the WriteFullEndElement methods.

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
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 'Main
End Class 'Sample

備註

如果項目不包含任何內容,短結束標記"/ > 「 寫入;否則,會寫入完整的結束標記。If the element contains no content, a short end tag "/>" is written; otherwise, a full end tag is written.

注意

當您使用XmlWriter將不會寫入至輸出 XML 中,項目和屬性的方法,直到您呼叫Close方法。When you use the XmlWriter methods to output XML, the elements and attributes will not be written until you call the Close method. 例如,如果您用來填入 XmlWriter XmlDocument,除非您關閉XmlWriter,不能觀察目標文件中的屬性與寫入的項目。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.

這個方法的非同步版本,請參閱WriteEndElementAsyncFor the asynchronous version of this method, see WriteEndElementAsync.

適用於