XmlWriter.WriteEndElement 方法

定义

当在派生类中被重写时,关闭一个元素并弹出相应的命名空间范围。

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

例外

这导致无效的 XML 文档。

- 或 -

在上一次异步操作完成之前调用了 XmlWriter 方法。 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”

示例

以下示例使用 WriteEndElementWriteFullEndElement 方法。

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

注解

如果 元素不包含任何内容,则写入短端标记“/>”;否则,将写入完整的结束标记。

备注

使用 XmlWriter 方法输出 XML 时,在调用 Close 方法之前不会写入元素和属性。 例如,如果在关闭 XmlWriter之前使用 XmlWriter 填充 XmlDocument,则将无法在目标文档中观察已写入的元素和属性。

有关此方法的异步版本,请参阅 WriteEndElementAsync

适用于