XmlWriter 类

定义

表示一个写入器,该写入器提供一种快速、非缓存和只进方式以生成包含 XML 数据的流或文件。Represents a writer that provides a fast, non-cached, forward-only way to generate streams or files that contain XML data.

public ref class XmlWriter abstract : IDisposable
public abstract class XmlWriter : IDisposable
type XmlWriter = class
    interface IDisposable
Public MustInherit Class XmlWriter
Implements IDisposable
继承
XmlWriter
派生
实现

示例

下面的示例代码演示如何使用异步 API 来生成 XML。The following example code shows how to use the asynchronous API to generate XML.

async Task TestWriter(Stream stream)   
{  
    XmlWriterSettings settings = new XmlWriterSettings();  
    settings.Async = true;  
  
    using (XmlWriter writer = XmlWriter.Create(stream, settings)) {  
        await writer.WriteStartElementAsync("pf", "root", "http://ns");  
        await writer.WriteStartElementAsync(null, "sub", null);  
        await writer.WriteAttributeStringAsync(null, "att", null, "val");  
        await writer.WriteStringAsync("text");  
        await writer.WriteEndElementAsync();  
        await writer.WriteProcessingInstructionAsync("pName", "pValue");  
        await writer.WriteCommentAsync("cValue");  
        await writer.WriteCDataAsync("cdata value");  
        await writer.WriteEndElementAsync();  
        await writer.FlushAsync();  
    }  
}  

注解

XmlWriter 类将 XML 数据写入流、文件、文本读取器或字符串。The XmlWriter class writes XML data to a stream, file, text reader, or string. 它支持 W3C可扩展标记语言(xml)1.0 (第四版)XML 1.0 (第三版)建议中的命名空间It supports the W3C Extensible Markup Language (XML) 1.0 (fourth edition) and Namespaces in XML 1.0 (third edition) recommendations.

使用 XmlWriter 类的成员可以:The members of the XmlWriter class enable you to:

  • 检查字符是不是合法的 XML 字符,元素和属性的名称是不是有效的 XML 名称。Verify that the characters are legal XML characters and that element and attribute names are valid XML names.

  • 检查 XML 文档的格式是否正确。Verify that the XML document is well-formed.

  • 将二进制字节编码为 Base64 或 BinHex,并写出结果文本。Encode binary bytes as Base64 or BinHex, and write out the resulting text.

  • 使用公共语言运行时类型而不是字符串传递值,以避免必须手动执行值转换。Pass values by using common language runtime types instead of strings, to avoid having to manually perform value conversions.

  • 将多个文档写入一个输出流。Write multiple documents to one output stream.

  • 写出有效的名称、限定名和名称标记。Write valid names, qualified names, and name tokens.

本节内容:In this section:

创建 XML 编写器 Creating an XML writer
指定输出格式 Specifying the output format
数据一致性 Data conformance
编写元素 Writing elements
编写属性 Writing attributes
处理命名空间 Handling namespaces
写入类型化数据 Writing typed data
关闭 XML 编写器 Closing the XML writer
异步编程 Asynchronous programming
安全注意事项Security considerations

创建 XML 编写器Creating an XML writer

若要创建 XmlWriter 实例,请使用 XmlWriter.Create 方法。To create an XmlWriter instance, use the XmlWriter.Create method. 若要指定要在 XML 编写器上启用的功能集,请向 Create 方法传递 XmlWriterSettingsTo specify the set of features you want to enable on the XML writer, pass an XmlWriterSettings to the Create method. 否则,将使用默认设置。Otherwise, default settings are used. 有关详细信息,请参阅 Create 参考页。See the Create reference pages for details.

指定输出格式Specifying the output format

XmlWriterSettings 类包含若干属性,这些属性控制 XmlWriter 输出的格式:The XmlWriterSettings class includes several properties that control how XmlWriter output is formatted:

属性Property 说明Description
Encoding 指定要使用的文本编码。Specifies the text encoding to use. 默认值为 Encoding.UTF8The default is Encoding.UTF8.
Indent 指示是否使元素缩进。Indicates whether to indent elements. 默认值为 false (无缩进)。The default is false (no indentation).
IndentChars 指定在缩进时要使用的字符串。Specifies the character string to use when indenting. 默认值为两个空格。The default is two spaces.
NewLineChars 指定分行符要使用的字符串。Specifies the character string to use for line breaks. 默认值为 \r\n(回车符、换行符)。The default is \r\n (carriage return, line feed).
NewLineHandling 指定如何处理换行符。Specifies how to handle newline characters.
NewLineOnAttributes 指示是否在新行上编写属性。Indicates whether to write attributes on a new line. 使用此属性时,应将 Indent 设置为 trueIndent should be set to true when using this property. 默认值为 falseThe default is false.
OmitXmlDeclaration 指示是否编写 XML 声明。Indicates whether to write an XML declaration. 默认值为 falseThe default is false.

IndentIndentChars 属性控制无效空白如何格式化。The Indent and IndentChars properties control how insignificant white space is formatted. 例如,若要缩进元素节点:For example, to indent element nodes:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";
XmlWriter writer = XmlWriter.Create("books.xml", settings);
Dim settings As New XmlWriterSettings()
settings.Indent = True
settings.IndentChars = vbTab
Dim writer As XmlWriter = XmlWriter.Create("books.xml", settings)

使用 NewLineOnAttributes 用一种额外的缩进级别来编写新行上的每个属性:Use the NewLineOnAttributes to write each attribute on a new line with one extra level of indentation:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;
XmlWriter writer = XmlWriter.Create("books.xml", settings);
Dim settings As New XmlWriterSettings()
settings.Indent = True
settings.NewLineOnAttributes = True
Dim writer As XmlWriter = XmlWriter.Create("books.xml", settings)

数据一致性Data conformance

XML 编写器使用 XmlWriterSettings 类中的两个属性来检查数据一致性:An XML writer uses two properties from the XmlWriterSettings class to check for data conformance:

  • CheckCharacters 属性指示 XML 编写器检查字符,如果任何字符在合法范围之外时引发 XmlException 异常,如 W3C 所定义。The CheckCharacters property instructs the XML writer to check characters and throw an XmlException exception if any characters are outside the legal range, as defined by the W3C.

  • ConformanceLevel 属性将 XML 编写器配置为检查写入的流是否符合由 W3C 定义的格式正确的 XML 1.0 文档或文档片段的规则。The ConformanceLevel property configures the XML writer to check that the stream being written complies with the rules for a well-formed XML 1.0 document or document fragment, as defined by the W3C. 下表介绍了这三个一致性级别。The three conformance levels are described in the following table. 默认值为 DocumentThe default is Document. 有关详细信息,请参阅 XmlWriterSettings.ConformanceLevel 属性和 System.Xml.ConformanceLevel 枚举。For details, see the XmlWriterSettings.ConformanceLevel property and the System.Xml.ConformanceLevel enumeration.

    级别Level 说明Description
    Document XML 输出符合格式正确的 XML 1.0 文档的规则,并可由任何符合要求的处理器处理。The XML output conforms to the rules for a well-formed XML 1.0 document and can be processed by any conforming processor.
    Fragment XML 输出符合格式正确的 XML 1.0 文档片段的规则。The XML output conforms to the rules for a well-formed XML 1.0 document fragment.
    Auto XML 编写器根据传入数据确定要应用的聘约确认检查的级别(文档或片段)。The XML writer determines which level of conformation checking to apply (document or fragment) based on the incoming data.

编写元素Writing elements

您可以使用以下 XmlWriter 方法来写入元素节点。You can use the following XmlWriter methods to write element nodes. 有关示例,请参阅列出的方法。For examples, see the methods listed.

使用Use 功能To
WriteElementString 写入整个元素节点,包括字符串值。Write an entire element node, including a string value.
WriteStartElement 通过使用多个方法调用来写入元素值。To write an element value by using multiple method calls. 例如,可以调用 WriteValue 来编写类型化的值,调用 WriteCharEntity 来编写字符实体,调用 WriteAttributeString 来编写属性,也可以编写子元素。For example, you can call WriteValue to write a typed value, WriteCharEntity to write a character entity, WriteAttributeString to write an attribute, or you can write a child element. 这是 WriteElementString 方法的更复杂的版本。This is a more sophisticated version of the WriteElementString method.

若要关闭元素,请调用 WriteEndElementWriteFullEndElement 方法。To close the element, you call the WriteEndElement or WriteFullEndElement method.
WriteNode 复制在 XmlReaderXPathNavigator 对象当前位置找到的元素节点。To copy an element node found at the current position of an XmlReader or XPathNavigator object. 在调用时,会将源对象中的所有内容复制到 XmlWriter 实例。When called, it copies everything from the source object to the XmlWriter instance.

编写属性Writing attributes

您可以使用以下 XmlWriter 方法来写入元素节点上的属性。You can use the following XmlWriter methods to write attributes on element nodes. 这些方法还可用于在元素上创建命名空间声明,如下一部分中所述。These methods can also be used to create namespace declarations on an element, as discussed in the next section.

使用Use 功能To
WriteAttributeString 写入整个属性节点,包括字符串值。To write an entire attribute node, including a string value.
WriteStartAttribute 使用多个方法调用编写特性值。To write the attribute value using multiple method calls. 例如,可以调用 WriteValue 来编写类型化值。For example, you can call WriteValue to write a typed value. 这是 WriteElementString 方法的更复杂的版本。This is a more sophisticated version of the WriteElementString method.

若要关闭元素,请调用 WriteEndAttribute 方法。To close the element, you call the WriteEndAttribute method.
WriteAttributes 复制 XmlReader 对象当前位置找到的所有属性。To copy all the attributes found at the current position of an XmlReader object. 写入的属性取决于读取器当前所处节点的类型:The attributes that are written depend on the type of node the reader is currently positioned on:

-对于特性节点,它将写入当前特性,然后写入特性的其余部分,直到元素结束标记为止。- For an attribute node, it writes the current attribute, and then the rest of the attributes until the element closing tag.
-对于元素节点,它将写入元素包含的所有属性。- For an element node, it writes all attributes contained by the element.
-对于 XML 声明节点,它将写入声明中的所有属性。- For an XML declaration node, it writes all the attributes in the declaration.
-对于所有其他节点类型,该方法将引发异常。- For all other node types, the method throws an exception.

处理命名空间Handling namespaces

命名空间用于限定 XML 文档中的元素和属性的名称。Namespaces are used to qualify element and attribute names in an XML document. 命名空间前缀将元素和属性与命名空间关联,后者又与 URI 引用关联。Namespace prefixes associate elements and attributes with namespaces, which are in turn associated with URI references. 命名空间在 XML 文档中实现元素和属性名称的唯一性。Namespaces create element and attribute name uniqueness in an XML document.

XmlWriter 维护与当前命名空间范围中定义的所有命名空间对应的命名空间堆栈。The XmlWriter maintains a namespace stack that corresponds to all the namespaces defined in the current namespace scope. 在编写元素和属性时,可以通过下列方式利用命名空间:When writing elements and attributes you can utilize namespaces in the following ways:

  • 使用 WriteAttributeString 方法手动声明命名空间。Declare namespaces manually by using the WriteAttributeString method. 如果知道如何最大程度地优化命名空间声明的数目,可以使用此方法。This can be useful when you know how to best optimize the number of namespace declarations. 有关示例,请参阅 WriteAttributeString(String, String, String, String) 方法。For an example, see the WriteAttributeString(String, String, String, String) method.

  • 用新的命名空间重写当前命名空间声明。Override the current namespace declaration with a new namespace. 在下面的代码中,WriteAttributeString 方法将 "x" 前缀的命名空间 URI 从 "123" 更改为 "abc"In the following code, the WriteAttributeString method changes the namespace URI for the "x" prefix from "123" to "abc".

    writer.WriteStartElement("x", "root", "123");
    writer.WriteStartElement("item");
    writer.WriteAttributeString("xmlns", "x", null, "abc");
    writer.WriteEndElement();
    writer.WriteEndElement();
    
    writer.WriteStartElement("x", "root", "123")
    writer.WriteStartElement("item")
    writer.WriteAttributeString("xmlns", "x", Nothing, "abc")
    writer.WriteEndElement()
    writer.WriteEndElement()
    

    代码生成以下 XML 字符串:The code generates the following XML string:

    <x:root xmlns:x="123">  
      <item xmlns:x="abc" />  
    </x:root>  
    
  • 在编写属性或元素时指定命名空间前缀。Specify a namespace prefix when writing attributes or elements. 许多用于写入元素和属性的方法使您能够执行此操作。Many of the methods used to write element and attributes enable you to do this. 例如,WriteStartElement(String, String, String) 方法写入开始标记并将其与指定的命名空间和前缀关联。For example, the WriteStartElement(String, String, String) method writes a start tag and associates it with a specified namespace and prefix.

写入类型化数据Writing typed data

WriteValue 方法接受公共语言运行时(CLR)对象,根据 XML 架构定义语言(XSD)数据类型转换规则将输入值转换为其字符串表示形式,并通过使用 WriteString 方法将其写入。The WriteValue method accepts a common language runtime (CLR) object, converts the input value to its string representation according to XML schema definition language (XSD) data type conversion rules, and writes it out by using the WriteString method. 这比使用 XmlConvert 类中的方法在写出类型化数据之前将其转换为字符串值更容易。This is easier than using the methods in the XmlConvert class to convert the typed data to a string value before writing it out.

写入文本时,键入的值将通过使用该架构类型的 XmlConvert 规则序列化为文本。When writing to text, the typed value is serialized to text by using the XmlConvert rules for that schema type.

有关对应于 CLR 类型的默认 XSD 数据类型,请参阅 WriteValue 方法。For default XSD data types that correspond to CLR types, see the WriteValue method.

XmlWriter 还可用于写入到 XML 数据存储区。The XmlWriter can also be used to write to an XML data store. 例如,XPathNavigator 类可以创建一个 XmlWriter 对象,用于为 XmlDocument 对象创建节点。For example, the XPathNavigator class can create an XmlWriter object to create nodes for an XmlDocument object. 如果数据存储具有可供其使用的架构信息,则如果尝试转换为不允许的类型,WriteValue 方法将引发异常。如果数据存储没有可用的架构信息,WriteValue 方法会将所有值视为 xsd:anySimpleType 类型。If the data store has schema information available to it, the WriteValue method throws an exception if you try to convert to a type that is not allowed.If the data store does not have schema information available to it, the WriteValue method treats all values as an xsd:anySimpleType type.

关闭 XML 编写器Closing the XML writer

使用 XmlWriter 方法输出 XML 时,在调用 Close 方法之前,不会写入元素和特性。When you use XmlWriter methods to output XML, the elements and attributes are not written until you call the Close method. 例如,如果使用 XmlWriter 填充 XmlDocument 对象,则在关闭 XmlWriter 实例之前,将无法在目标文档中看到已写入的元素和属性。For example, if you are using XmlWriter to populate an XmlDocument object, you won't be able to see the written elements and attributes in the target document until you close the XmlWriter instance.

异步编程Asynchronous programming

大多数 XmlWriter 方法的方法名称末尾都有 "Async"。Most of the XmlWriter methods have asynchronous counterparts that have "Async" at the end of their method names. 例如,WriteAttributeStringAsyncWriteAttributeString 的异步等效项。For example, the asynchronous equivalent of WriteAttributeString is WriteAttributeStringAsync.

对于不包含异步对应的 WriteValue 方法,请将返回值转换为字符串并改用 WriteStringAsync 方法。For the WriteValue method, which doesn't have an asynchronous counterpart, convert the return value to a string and use the WriteStringAsync method instead.

安全注意事项Security considerations

使用 XmlWriter 类时,请注意以下事项:Consider the following when working with the XmlWriter class:

  • XmlWriter 引发的异常可能会泄露您不希望冒泡到应用程序的路径信息。Exceptions thrown by the XmlWriter can disclose path information that you do not want bubbled up to the app. 应用必须捕获异常并对其进行相应处理。Your app must catch exceptions and process them appropriately.

  • XmlWriter 不会验证传递到 WriteDocTypeWriteRaw 方法的任何数据。XmlWriter does not validate any data that is passed to the WriteDocType or WriteRaw method. 不应将任意数据传递给这些方法。You should not pass arbitrary data to these methods.

构造函数

XmlWriter()

初始化 XmlWriter 类的新实例。Initializes a new instance of the XmlWriter class.

属性

Settings

获取用于创建此 XmlWriter 实例的 XmlWriterSettings 对象。Gets the XmlWriterSettings object used to create this XmlWriter instance.

WriteState

当在派生类中被重写时,获取写入器的状态。When overridden in a derived class, gets the state of the writer.

XmlLang

当在派生类中被重写时,获取当前的 xml:lang 范围。When overridden in a derived class, gets the current xml:lang scope.

XmlSpace

当在派生类中被重写时,获取表示当前 xml:space 范围的 XmlSpaceWhen overridden in a derived class, gets an XmlSpace representing the current xml:space scope.

方法

Close()

当在派生类中被重写时,将关闭此流和基础流。When overridden in a derived class, closes this stream and the underlying stream.

Create(Stream)

使用指定的流创建一个新的 XmlWriter 实例。Creates a new XmlWriter instance using the specified stream.

Create(Stream, XmlWriterSettings)

使用流和 XmlWriter 对象创建一个新的 XmlWriterSettings 实例。Creates a new XmlWriter instance using the stream and XmlWriterSettings object.

Create(String)

使用指定的文件名创建一个新的 XmlWriter 实例。Creates a new XmlWriter instance using the specified filename.

Create(String, XmlWriterSettings)

使用文件名和 XmlWriter 对象创建一个新的 XmlWriterSettings 实例。Creates a new XmlWriter instance using the filename and XmlWriterSettings object.

Create(StringBuilder)

使用指定的 XmlWriter 创建一个新的 StringBuilder 实例。Creates a new XmlWriter instance using the specified StringBuilder.

Create(StringBuilder, XmlWriterSettings)

使用 XmlWriterStringBuilder 对象创建一个新的 XmlWriterSettings 实例。Creates a new XmlWriter instance using the StringBuilder and XmlWriterSettings objects.

Create(TextWriter)

使用指定的 XmlWriter 创建一个新的 TextWriter 实例。Creates a new XmlWriter instance using the specified TextWriter.

Create(TextWriter, XmlWriterSettings)

使用 XmlWriterTextWriter 对象创建一个新的 XmlWriterSettings 实例。Creates a new XmlWriter instance using the TextWriter and XmlWriterSettings objects.

Create(XmlWriter)

使用指定的 XmlWriter 对象创建一个新的 XmlWriter 实例。Creates a new XmlWriter instance using the specified XmlWriter object.

Create(XmlWriter, XmlWriterSettings)

使用指定的 XmlWriterXmlWriter 对象创建一个新的 XmlWriterSettings 实例。Creates a new XmlWriter instance using the specified XmlWriter and XmlWriterSettings objects.

Dispose()

释放 XmlWriter 类的当前实例所使用的所有资源。Releases all resources used by the current instance of the XmlWriter class.

Dispose(Boolean)

释放由 XmlWriter 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the XmlWriter and optionally releases the managed resources.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
Flush()

当在派生类中被重写时,将缓冲区中的所有内容刷新到基础流,并同时刷新基础流。When overridden in a derived class, flushes whatever is in the buffer to the underlying streams and also flushes the underlying stream.

FlushAsync()

将缓冲区中的所有内容异步刷新到基础流,并同时刷新基础流。Asynchronously flushes whatever is in the buffer to the underlying streams and also flushes the underlying stream.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
LookupPrefix(String)

当在派生类中被重写时,返回在当前命名空间范围中为该命名空间 URI 定义的最近的前缀。When overridden in a derived class, returns the closest prefix defined in the current namespace scope for the namespace URI.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
WriteAttributes(XmlReader, Boolean)

当在派生类中被重写时,写出在 XmlReader 中当前位置找到的所有属性。When overridden in a derived class, writes out all the attributes found at the current position in the XmlReader.

WriteAttributesAsync(XmlReader, Boolean)

XmlReader 中的当前位置异步写出找到的所有属性。Asynchronously writes out all the attributes found at the current position in the XmlReader.

WriteAttributeString(String, String)

当在派生类中被重写时,写出具有指定的本地名称和值的属性。When overridden in a derived class, writes out the attribute with the specified local name and value.

WriteAttributeString(String, String, String)

当在派生类中被重写时,写入具有指定的本地名称、命名空间 URI 和值的属性。When overridden in a derived class, writes an attribute with the specified local name, namespace URI, and value.

WriteAttributeString(String, String, String, String)

当在派生类中被重写时,写出具有指定的前缀、本地名称、命名空间 URI 和值的属性。When overridden in a derived class, writes out the attribute with the specified prefix, local name, namespace URI, and value.

WriteAttributeStringAsync(String, String, String, String)

异步写出具有指定前缀、本地名称、命名空间 URI 和值的属性。Asynchronously writes out the attribute with the specified prefix, local name, namespace URI, and value.

WriteBase64(Byte[], Int32, Int32)

当在派生类中被重写时,将指定的二进制字节编码为 Base64 并写出结果文本。When overridden in a derived class, encodes the specified binary bytes as Base64 and writes out the resulting text.

WriteBase64Async(Byte[], Int32, Int32)

将指定的二进制字节异步编码为 Base64 并写出结果文本。Asynchronously encodes the specified binary bytes as Base64 and writes out the resulting text.

WriteBinHex(Byte[], Int32, Int32)

当在派生类中被重写时,将指定的二进制字节编码为 BinHex 并写出结果文本。When overridden in a derived class, encodes the specified binary bytes as BinHex and writes out the resulting text.

WriteBinHexAsync(Byte[], Int32, Int32)

将指定的二进制字节异步编码为 BinHex 并写出结果文本。Asynchronously encodes the specified binary bytes as BinHex and writes out the resulting text.

WriteCData(String)

当在派生类中被重写时,写出包含指定文本的 <![CDATA[...]]> 块。When overridden in a derived class, writes out a <![CDATA[...]]> block containing the specified text.

WriteCDataAsync(String)

异步写出一个包含指定文本的 <![CDATA[...]]> 块。Asynchronously writes out a <![CDATA[...]]> block containing the specified text.

WriteCharEntity(Char)

当在派生类中被重写时,为指定的 Unicode 字符值强制生成字符实体。When overridden in a derived class, forces the generation of a character entity for the specified Unicode character value.

WriteCharEntityAsync(Char)

为指定的 Unicode 字符值异步强制生成字符实体。Asynchronously forces the generation of a character entity for the specified Unicode character value.

WriteChars(Char[], Int32, Int32)

当在派生类中被重写时,以每次一个缓冲区的方式写入文本。When overridden in a derived class, writes text one buffer at a time.

WriteCharsAsync(Char[], Int32, Int32)

以每次一个缓冲区的方式异步写入文本。Asynchronously writes text one buffer at a time.

WriteComment(String)

当在派生类中被重写时,写出包含指定文本的注释 <!--...-->。When overridden in a derived class, writes out a comment <!--...--> containing the specified text.

WriteCommentAsync(String)

异步写出一个包含指定文本的注释 <!--...-->。Asynchronously writes out a comment <!--...--> containing the specified text.

WriteDocType(String, String, String, String)

当在派生类中被重写时,写出具有指定名称和可选属性的 DOCTYPE 声明。When overridden in a derived class, writes the DOCTYPE declaration with the specified name and optional attributes.

WriteDocTypeAsync(String, String, String, String)

异步写入具有指定名称和可选属性的 DOCTYPE 声明。Asynchronously writes the DOCTYPE declaration with the specified name and optional attributes.

WriteElementString(String, String)

写入具有指定的本地名称和值的元素。Writes an element with the specified local name and value.

WriteElementString(String, String, String)

写入具有指定的本地名称、命名空间 URI 和值的元素。Writes an element with the specified local name, namespace URI, and value.

WriteElementString(String, String, String, String)

写入具有指定的前缀、本地名称、命名空间 URI 和值的元素。Writes an element with the specified prefix, local name, namespace URI, and value.

WriteElementStringAsync(String, String, String, String)

异步写入具有指定的前缀、本地名称、命名空间 URI 和值的元素。Asynchronously writes an element with the specified prefix, local name, namespace URI, and value.

WriteEndAttribute()

当在派生类中被重写时,关闭上一个 WriteStartAttribute(String, String) 调用。When overridden in a derived class, closes the previous WriteStartAttribute(String, String) call.

WriteEndAttributeAsync()

异步关闭前一个 WriteStartAttribute(String, String) 调用。Asynchronously closes the previous WriteStartAttribute(String, String) call.

WriteEndDocument()

当在派生类中被重写时,关闭任何打开的元素或属性并将写入器重新设置为起始状态。When overridden in a derived class, closes any open elements or attributes and puts the writer back in the Start state.

WriteEndDocumentAsync()

异步关闭任何打开的元素或属性并将写入器重新设置为起始状态。Asynchronously closes any open elements or attributes and puts the writer back in the Start state.

WriteEndElement()

当在派生类中被重写时,关闭一个元素并弹出相应的命名空间范围。When overridden in a derived class, closes one element and pops the corresponding namespace scope.

WriteEndElementAsync()

异步关闭一个元素并弹出相应的命名空间范围。Asynchronously closes one element and pops the corresponding namespace scope.

WriteEntityRef(String)

当在派生类中被重写时,按 &name; 写出实体引用。When overridden in a derived class, writes out an entity reference as &name;.

WriteEntityRefAsync(String)

&name; 异步写出实体引用。Asynchronously writes out an entity reference as &name;.

WriteFullEndElement()

当在派生类中被重写时,关闭一个元素并弹出相应的命名空间范围。When overridden in a derived class, closes one element and pops the corresponding namespace scope.

WriteFullEndElementAsync()

异步关闭一个元素并弹出相应的命名空间范围。Asynchronously closes one element and pops the corresponding namespace scope.

WriteName(String)

当在派生类中被重写时,写出指定的名称,确保它是符合 W3C XML 1.0 建议 (https://www.w3.org/TR/1998/REC-xml-19980210#NT-Name)) 的有效名称。When overridden in a derived class, writes out the specified name, ensuring it is a valid name according to the W3C XML 1.0 recommendation (https://www.w3.org/TR/1998/REC-xml-19980210#NT-Name).

WriteNameAsync(String)

异步写出指定的名称,确保它是符合 W3C XML 1.0 建议 (https://www.w3.org/TR/1998/REC-xml-19980210#NT-Name)) 的有效名称。Asynchronously writes out the specified name, ensuring it is a valid name according to the W3C XML 1.0 recommendation (https://www.w3.org/TR/1998/REC-xml-19980210#NT-Name).

WriteNmToken(String)

当在派生类中被重写时,写出指定的名称,确保它是符合 W3C XML 1.0 建议 (https://www.w3.org/TR/1998/REC-xml-19980210#NT-Name)) 的有效 NmToken。When overridden in a derived class, writes out the specified name, ensuring it is a valid NmToken according to the W3C XML 1.0 recommendation (https://www.w3.org/TR/1998/REC-xml-19980210#NT-Name).

WriteNmTokenAsync(String)

异步写出指定的名称,确保它是符合 W3C XML 1.0 建议 (https://www.w3.org/TR/1998/REC-xml-19980210#NT-Name)) 的有效 NmToken。Asynchronously writes out the specified name, ensuring it is a valid NmToken according to the W3C XML 1.0 recommendation (https://www.w3.org/TR/1998/REC-xml-19980210#NT-Name).

WriteNode(XmlReader, Boolean)

当在派生类中被重写时,将全部内容从读取器复制到写入器并将读取器移动到下一个同级的开始位置。When overridden in a derived class, copies everything from the reader to the writer and moves the reader to the start of the next sibling.

WriteNode(XPathNavigator, Boolean)

将所有内容从 XPathNavigator 对象复制到编写器。Copies everything from the XPathNavigator object to the writer. XPathNavigator 的位置保持不变。The position of the XPathNavigator remains unchanged.

WriteNodeAsync(XmlReader, Boolean)

将所有内容从读取器异步复制到写入器并将读取器移动到下一个同级的开头。Asynchronously copies everything from the reader to the writer and moves the reader to the start of the next sibling.

WriteNodeAsync(XPathNavigator, Boolean)

从编写器的 XPathNavigator 对象中异步复制所有内容。Asynchronously copies everything from the XPathNavigator object to the writer. XPathNavigator 的位置保持不变。The position of the XPathNavigator remains unchanged.

WriteProcessingInstruction(String, String)

当在派生类中被重写时,写出在名称和文本之间带有空格的处理指令,如下所示:<?name text?>。When overridden in a derived class, writes out a processing instruction with a space between the name and text as follows: <?name text?>.

WriteProcessingInstructionAsync(String, String)

异步写出在名称和文本之间有空格的处理指令,如下所示:<?name text?>。Asynchronously writes out a processing instruction with a space between the name and text as follows: <?name text?>.

WriteQualifiedName(String, String)

当在派生类中被重写时,写出命名空间限定的名称。When overridden in a derived class, writes out the namespace-qualified name. 此方法查找位于给定命名空间范围内的前缀。This method looks up the prefix that is in scope for the given namespace.

WriteQualifiedNameAsync(String, String)

异步写出命名空间限定的名称。Asynchronously writes out the namespace-qualified name. 此方法查找位于给定命名空间范围内的前缀。This method looks up the prefix that is in scope for the given namespace.

WriteRaw(Char[], Int32, Int32)

当在派生类中被重写时,从字符缓冲区手动写入原始标记。When overridden in a derived class, writes raw markup manually from a character buffer.

WriteRaw(String)

当在派生类中被重写时,从字符串手动写入原始标记。When overridden in a derived class, writes raw markup manually from a string.

WriteRawAsync(Char[], Int32, Int32)

从字符缓冲区手动异步写入原始标记。Asynchronously writes raw markup manually from a character buffer.

WriteRawAsync(String)

从字符串手动异步写入原始标记。Asynchronously writes raw markup manually from a string.

WriteStartAttribute(String)

写入具有指定本地名称的属性的开头。Writes the start of an attribute with the specified local name.

WriteStartAttribute(String, String)

写入具有指定本地名称和命名空间 URI 的属性的开头。Writes the start of an attribute with the specified local name and namespace URI.

WriteStartAttribute(String, String, String)

当在派生类中被重写时,写入具有指定的前缀、本地名称和命名空间 URI 的属性的开头。When overridden in a derived class, writes the start of an attribute with the specified prefix, local name, and namespace URI.

WriteStartAttributeAsync(String, String, String)

异步写入具有指定前缀、本地名称和命名空间 URI 的属性的开头。Asynchronously writes the start of an attribute with the specified prefix, local name, and namespace URI.

WriteStartDocument()

当在派生类中被重写时,写入版本为“1.0”的 XML 声明。When overridden in a derived class, writes the XML declaration with the version "1.0".

WriteStartDocument(Boolean)

当在派生类中被重写时,写入版本为“1.0”的 XML 声明和独立的属性。When overridden in a derived class, writes the XML declaration with the version "1.0" and the standalone attribute.

WriteStartDocumentAsync()

异步写入版本为“1.0”的 XML 声明。Asynchronously writes the XML declaration with the version "1.0".

WriteStartDocumentAsync(Boolean)

异步写入版本为“1.0”的 XML 声明和独立的属性。Asynchronously writes the XML declaration with the version "1.0" and the standalone attribute.

WriteStartElement(String)

当在派生类中被重写时,写出具有指定的本地名称的开始标记。When overridden in a derived class, writes out a start tag with the specified local name.

WriteStartElement(String, String)

当在派生类中被重写时,写入指定的开始标记并将其与给定的命名空间关联起来。When overridden in a derived class, writes the specified start tag and associates it with the given namespace.

WriteStartElement(String, String, String)

当在派生类中被重写时,写入指定的开始标记并将其与给定的命名空间和前缀关联起来。When overridden in a derived class, writes the specified start tag and associates it with the given namespace and prefix.

WriteStartElementAsync(String, String, String)

异步写入指定的开始标记并将其与给定的命名空间和前缀关联起来。Asynchronously writes the specified start tag and associates it with the given namespace and prefix.

WriteString(String)

当在派生类中被重写时,写入给定的文本内容。When overridden in a derived class, writes the given text content.

WriteStringAsync(String)

异步写入给定的文本内容。Asynchronously writes the given text content.

WriteSurrogateCharEntity(Char, Char)

当在派生类中被重写时,为代理项字符对生成并写入代理项字符实体。When overridden in a derived class, generates and writes the surrogate character entity for the surrogate character pair.

WriteSurrogateCharEntityAsync(Char, Char)

为代理项字符对异步生成并写入代理项字符实体。Asynchronously generates and writes the surrogate character entity for the surrogate character pair.

WriteValue(Boolean)

写入 Boolean 值。Writes a Boolean value.

WriteValue(DateTime)

写入 DateTime 值。Writes a DateTime value.

WriteValue(DateTimeOffset)

写入 DateTimeOffset 值。Writes a DateTimeOffset value.

WriteValue(Decimal)

写入 Decimal 值。Writes a Decimal value.

WriteValue(Double)

写入 Double 值。Writes a Double value.

WriteValue(Int32)

写入 Int32 值。Writes a Int32 value.

WriteValue(Int64)

写入 Int64 值。Writes a Int64 value.

WriteValue(Object)

写入对象值。Writes the object value.

WriteValue(Single)

写入一个单精度浮点数。Writes a single-precision floating-point number.

WriteValue(String)

写入 String 值。Writes a String value.

WriteWhitespace(String)

当在派生类中被重写时,写出给定的空白区域。When overridden in a derived class, writes out the given white space.

WriteWhitespaceAsync(String)

异步写出给定的空白区域。Asynchronously writes out the given white space.

显式界面实现

IDisposable.Dispose()

有关此成员的说明,请参见 Dispose()For a description of this member, see Dispose().

适用于

另请参阅