XmlWriter.WriteEndElement 方法

定義

在衍生類別中覆寫時,關閉一個項目並取出對應的命名空間範圍。

C#
public abstract void WriteEndElement ();

例外狀況

這會導致無效的 XML 文件。

-或-

在先前的非同步作業完成前呼叫了 XmlWriter 方法。 在此情況下,會擲回 InvalidOperationException 與「非同步作業已經在進行中」的訊息。

範例

下列範例會使用 WriteEndElementWriteFullEndElement 方法。

C#
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();
  }
}

備註

如果專案不包含任何內容,則會寫入簡短的標籤 「/>」 ;否則,會寫入完整結束標記。

注意

當您使用 XmlWriter 方法來輸出 XML 時,除非您呼叫 Close 方法,否則不會寫入元素和屬性。 例如,如果您使用 XmlWriter 來填入 XmlDocument ,直到您關閉 XmlWriter 為止,您將無法觀察目的檔案中的寫入元素和屬性。

如需這個方法的非同步版本,請參閱 WriteEndElementAsync

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0