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之前使用 XmlWriter 填充 XmlDocument,则将无法在目标文档中观察已写入的元素和属性。

有关此方法的异步版本,请参阅 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, 8, 9, 10
.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, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0