XmlWriter.WriteStartElement 方法

定义

当在派生类中被重写时,写入指定的开始标记。

重载

WriteStartElement(String)

当在派生类中被重写时,写出具有指定的本地名称的开始标记。

WriteStartElement(String, String)

当在派生类中被重写时,写入指定的开始标记并将其与给定的命名空间关联起来。

WriteStartElement(String, String, String)

当在派生类中被重写时,写入指定的开始标记并将其与给定的命名空间和前缀关联起来。

注解

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

WriteStartElement(String)

Source:
XmlWriter.cs
Source:
XmlWriter.cs
Source:
XmlWriter.cs

当在派生类中被重写时,写出具有指定的本地名称的开始标记。

C#
public void WriteStartElement (string localName);

参数

localName
String

元素的本地名称。

例外

编写器已关闭。

- 或 -

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

缓冲区中有一个字符是有效的 XML 字符,但对于输出编码是无效的。 例如,如果输出编码为 ASCII,应该仅对元素和特性名使用从 0 到 127 范围内的字符。 无效的字符可能位于此方法的参数中,或者位于以前要写入缓冲区的方法的参数中。 如果可能,此类字符将使用字符实体引用进行转义(例如,在文本节点或特性值中)。 但是,不允许在元素名、特性名、注释、处理指令和 CDATA 节中使用字符实体引用。

示例

以下示例写入一个 XML 节点。

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;
     settings.OmitXmlDeclaration = true;
     XmlWriter writer = XmlWriter.Create(Console.Out, settings);

     // Write the book element.
     writer.WriteStartElement("book");

     // Write the title element.
     writer.WriteStartElement("title");
     writer.WriteString("Pride And Prejudice");
     writer.WriteEndElement();

     // Write the close tag for the root element.
     writer.WriteEndElement();

     // Write the XML and close the writer.
     writer.Close();
  }
}

注解

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

适用于

.NET 9 和其他版本
产品 版本
.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
.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

WriteStartElement(String, String)

Source:
XmlWriter.cs
Source:
XmlWriter.cs
Source:
XmlWriter.cs

当在派生类中被重写时,写入指定的开始标记并将其与给定的命名空间关联起来。

C#
public void WriteStartElement (string localName, string ns);
C#
public void WriteStartElement (string localName, string? ns);

参数

localName
String

元素的本地名称。

ns
String

与元素关联的命名空间 URI。 如果此命名空间已在范围中并具有关联的前缀,则写入器也将自动写入该前缀。

例外

编写器已关闭。

- 或 -

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

缓冲区中有一个字符是有效的 XML 字符,但对于输出编码是无效的。 例如,如果输出编码为 ASCII,应该仅对元素和特性名使用从 0 到 127 范围内的字符。 无效的字符可能位于此方法的参数中,或者位于以前要写入缓冲区的方法的参数中。 如果可能,此类字符将使用字符实体引用进行转义(例如,在文本节点或特性值中)。 但是,不允许在元素名、特性名、注释、处理指令和 CDATA 节中使用字符实体引用。

注解

调用此方法后,可以编写属性,或使用 、 WriteStringWriteStartElement 为子元素创建内容WriteComment。 可以使用 或 WriteFullEndElement关闭 元素WriteEndElement。 例如,以下 C# 代码:

C#
writer.WriteStartElement("item",null);
writer.WriteString("some text");
writer.WriteEndElement();

生成以下输出:

XML
<item>some text</item>

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

适用于

.NET 9 和其他版本
产品 版本
.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
.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

WriteStartElement(String, String, String)

Source:
XmlWriter.cs
Source:
XmlWriter.cs
Source:
XmlWriter.cs

当在派生类中被重写时,写入指定的开始标记并将其与给定的命名空间和前缀关联起来。

C#
public abstract void WriteStartElement (string prefix, string localName, string ns);
C#
public abstract void WriteStartElement (string? prefix, string localName, string? ns);

参数

prefix
String

元素的命名空间前缀。

localName
String

元素的本地名称。

ns
String

与元素关联的命名空间 URI。

例外

编写器已关闭。

- 或 -

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

缓冲区中有一个字符是有效的 XML 字符,但对于输出编码是无效的。 例如,如果输出编码为 ASCII,应该仅对元素和特性名使用从 0 到 127 范围内的字符。 无效的字符可能位于此方法的参数中,或者位于以前要写入缓冲区的方法的参数中。 如果可能,此类字符将使用字符实体引用进行转义(例如,在文本节点或特性值中)。 但是,不允许在元素名、特性名、注释、处理指令和 CDATA 节中使用字符实体引用。

示例

以下示例写出一个 XML 片段。

C#
using System;
using System.IO;
using System.Xml;

 public class Sample
 {
   private const string m_Document = "sampledata.xml";

   public static void Main() {

      XmlWriter writer = null;

      try {

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        writer = XmlWriter.Create (m_Document, settings);

        writer.WriteComment("sample XML fragment");

        // Write an element (this one is the root).
        writer.WriteStartElement("book");

        // Write the namespace declaration.
        writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");

        // Write the genre attribute.
        writer.WriteAttributeString("genre", "novel");

        // Write the title.
        writer.WriteStartElement("title");
        writer.WriteString("The Handmaid's Tale");
        writer.WriteEndElement();

        // Write the price.
        writer.WriteElementString("price", "19.95");

        // Lookup the prefix and write the ISBN element.
        string prefix = writer.LookupPrefix("urn:samples");
        writer.WriteStartElement(prefix, "ISBN", "urn:samples");
        writer.WriteString("1-861003-78");
        writer.WriteEndElement();

        // Write the style element (shows a different way to handle prefixes).
        writer.WriteElementString("style", "urn:samples", "hardcover");

        // Write the close tag for the root element.
        writer.WriteEndElement();

        // Write the XML to file and close the writer.
        writer.Flush();
        writer.Close();
      }

      finally {
        if (writer != null)
           writer.Close();
     }
   }
 }

注解

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

适用于

.NET 9 和其他版本
产品 版本
.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
.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