XmlWriter.WriteValue 方法
定义
写入单一的简单类型化值。Writes a single simple-typed value.
重载
| WriteValue(Single) |
写入一个单精度浮点数。Writes a single-precision floating-point number. |
| WriteValue(Object) |
写入对象值。Writes the object value. |
| WriteValue(Int64) | |
| WriteValue(Int32) | |
| WriteValue(Double) | |
| WriteValue(String) | |
| WriteValue(DateTimeOffset) |
写入 DateTimeOffset 值。Writes a DateTimeOffset value. |
| WriteValue(DateTime) | |
| WriteValue(Boolean) | |
| WriteValue(Decimal) |
注解
WriteValue方法接受 (CLR) 的公共语言运行时,根据 XML 架构定义语言 (XSD) 数据类型转换规则,将它们转换为其字符串表示形式,并使用方法将这些值写入它们 WriteString 。The WriteValue method accepts common language runtime (CLR) simple-typed values, converts them to their string representations according to the XML schema definition language (XSD) data type conversion rules, and writes them out by using the WriteString method. 这使你能够在 XPathDocument 、和对象之间传递数据时,以最小的重新分析开销来管道简单类型的值 XmlReader XmlWriter 。This enables you to pipeline simple-type values with minimal reparsing overhead when passing data between XPathDocument, XmlReader, and XmlWriter objects. 当处理 CLR 简单类型和实例时,此功能也很有用 XmlWriter 。This ability is also useful when dealing with CLR simple-types and an XmlWriter instance. 您可以调用 WriteValue 方法来编写类型化的值,而不是使用类中的方法 XmlConvert 将类型化数据转换为字符串值,然后再将其写入。You can call the WriteValue method to write the typed value instead of using the methods in the XmlConvert class to convert the typed data to a string value before writing it out.
对于异步操作,将的返回值转换 WriteValue 为字符串,并使用 WriteStringAsync 方法。For asynchronous operations, convert the return value of WriteValue to a string and use the WriteStringAsync method.
下表显示了对应于 CLR 类型的默认 XSD 数据类型。The following table shows the default XSD data types that correspond to CLR types.
如果 CLR 对象是列表类型,例如 IEnumerable、IList 或 ICollection,将作为值类型数组对待。If the CLR object is a list type, such as IEnumerable, IList, or ICollection, it is treated as an array of the value type.
| CLR 类型CLR type | 默认 XSD 数据类型Default XSD data type |
|---|---|
| System.Boolean | xsd:booleanxsd:boolean |
| System.Byte** | xsd:integerxsd:integer |
| System.Byte 数组System.Byte array | xsd:base64Binaryxsd:base64Binary |
| System.Char** | xsd:stringxsd:string |
| System.DateTime | xsd:dateTimexsd:dateTime |
| System.Decimal | xsd:decimalxsd:decimal |
| System.Double | xsd:doublexsd:double |
| System.Int16** | xsd:integerxsd:integer |
| System.Int32 | xsd:integerxsd:integer |
| System.Int64 | xsd:integerxsd:integer |
| System.Single | xsd:floatxsd:float |
| System.String | xsd:stringxsd:string |
| System.IO.TextReader | xsd:stringxsd:string |
| System.IO.BinaryReader | xsd:base64Binaryxsd:base64Binary |
**这些类型不符合 CLS。**These types are not CLS-compliant. 它们没有相应的 WriteValue 方法。They do not have corresponding WriteValue methods.
如果继续多次调用 WriteValue,值不会通过空格分隔。If WriteValue is called multiple times in succession, the values are not delimited by a space. 必须在调用 WriteWhitespace 之间调用 WriteValue,以插入空白。You must call WriteWhitespace between calls to WriteValue to insert white space.
WriteValue(Single)
写入一个单精度浮点数。Writes a single-precision floating-point number.
public:
virtual void WriteValue(float value);
public virtual void WriteValue (float value);
abstract member WriteValue : single -> unit
override this.WriteValue : single -> unit
Public Overridable Sub WriteValue (value As Single)
参数
- value
- Single
要写入的单精度浮点数。The single-precision floating-point number to write.
例外
指定了无效值。An invalid value was specified.
在上一次异步操作完成之前调用了 XmlWriter 方法。An XmlWriter method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."
注解
此方法获取公共语言运行时对象,并使用 XML 架构定义语言 (XSD) 数据类型转换规则将输入值转换为所需的输出类型。This method takes the common language runtime object and converts the input value to the desired output type using the XML Schema definition language (XSD) data type conversion rules.
在调用 WriteValue 方法时,XmlWriter 根据 XML 架构 (XSD) 数据类型定义将值转换为其字符串表示形式并使用 WriteString 方法写出。When the WriteValue method is called, the XmlWriter converts the value to its string representations according to the XML Schema (XSD) data type rules and writes it out using the WriteString method.
如果继续多次调用 WriteValue,值不会通过空格分隔。If WriteValue is called multiple times in succession, the values are not delimited by a space. 必须在调用 WriteWhitespace 之间调用 WriteValue,以插入空白。You must call WriteWhitespace between calls to WriteValue to insert white space. 将数组类型传递给 XmlWriter.WriteValue(Object) 重载以写出非重复值的数组。Pass an array type to the XmlWriter.WriteValue(Object) overload to write out an array of distinct values.
如果在输出纯文本的实例上调用此方法,则将 XmlWriter 类型化值序列化为文本。If this method is called on an XmlWriter instance that outputs text-only, the typed value is serialized to text.
对于异步操作,请将此方法的返回值转换为字符串,并使用 WriteStringAsync 方法。For asynchronous operations, convert the return value of this method to a string and use the WriteStringAsync method.
适用于
WriteValue(Object)
写入对象值。Writes the object value.
public:
virtual void WriteValue(System::Object ^ value);
public virtual void WriteValue (object value);
abstract member WriteValue : obj -> unit
override this.WriteValue : obj -> unit
Public Overridable Sub WriteValue (value As Object)
参数
- value
- Object
要写入的对象值。The object value to write.
注意:随着 .NET Framework 3.5 的发布,该方法接受将 DateTimeOffset 作为参数。Note With the release of the .NET Framework 3.5, this method accepts DateTimeOffset as a parameter.
例外
指定了无效值。An invalid value was specified.
value 为 null。The value is null.
编写器已关闭或处于错误状态。The writer is closed or in error state.
- 或 --or-
在上一次异步操作完成之前调用了 XmlWriter 方法。An XmlWriter method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."
注解
此方法采用公共语言运行时 (CLR) 对象,并使用 XML 架构定义语言 (XSD) 数据类型转换规则将输入值转换为所需的输出类型。This method takes the common language runtime (CLR) object and converts the input value to the desired output type using the XML Schema definition language (XSD) data type conversion rules.
如果 CLR 对象是列表类型(如 IEnumerable 、或) IList ,则将 ICollection 其视为值类型的数组。If the CLR object is a list type such as IEnumerable, IList, or ICollection, it is treated as an array of the value type. XmlWriter根据 XML 架构 (XSD) 数据类型规则,将值转换为其字符串表示形式,并使用方法将其写出 WriteString 。The XmlWriter converts the value to its string representations according to the XML Schema (XSD) data type rules and writes it out using the WriteString method.
如果在输出纯文本的实例上调用此方法,则将 XmlWriter 类型化值序列化为文本。If this method is called on an XmlWriter instance that outputs text-only, the typed value is serialized to text.
对于异步操作,请将此方法的返回值转换为字符串,并使用 WriteStringAsync 方法。For asynchronous operations, convert the return value of this method to a string and use the WriteStringAsync method.
适用于
WriteValue(Int64)
public:
virtual void WriteValue(long value);
public virtual void WriteValue (long value);
abstract member WriteValue : int64 -> unit
override this.WriteValue : int64 -> unit
Public Overridable Sub WriteValue (value As Long)
参数
例外
指定了无效值。An invalid value was specified.
在上一次异步操作完成之前调用了 XmlWriter 方法。An XmlWriter method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."
注解
此方法获取公共语言运行时对象,并使用 XML 架构定义语言 (XSD) 数据类型转换规则将输入值转换为所需的输出类型。This method takes the common language runtime object and converts the input value to the desired output type using the XML Schema definition language (XSD) data type conversion rules.
在调用 WriteValue 方法时,XmlWriter 根据 XML 架构 (XSD) 数据类型定义将值转换为其字符串表示形式并使用 WriteString 方法写出。When the WriteValue method is called, the XmlWriter converts the value to its string representations according to the XML Schema (XSD) data type rules and writes it out using the WriteString method.
如果继续多次调用 WriteValue,值不会通过空格分隔。If WriteValue is called multiple times in succession, the values are not delimited by a space. 必须在调用 WriteWhitespace 之间调用 WriteValue,以插入空白。You must call WriteWhitespace between calls to WriteValue to insert white space. 将数组类型传递给 XmlWriter.WriteValue(Object) 重载以写出非重复值的数组。Pass an array type to the XmlWriter.WriteValue(Object) overload to write out an array of distinct values.
如果在输出纯文本的实例上调用此方法,则将 XmlWriter 类型化值序列化为文本。If this method is called on an XmlWriter instance that outputs text-only, the typed value is serialized to text.
对于异步操作,请将此方法的返回值转换为字符串,并使用 WriteStringAsync 方法。For asynchronous operations, convert the return value of this method to a string and use the WriteStringAsync method.
适用于
WriteValue(Int32)
public:
virtual void WriteValue(int value);
public virtual void WriteValue (int value);
abstract member WriteValue : int -> unit
override this.WriteValue : int -> unit
Public Overridable Sub WriteValue (value As Integer)
参数
例外
指定了无效值。An invalid value was specified.
在上一次异步操作完成之前调用了 XmlWriter 方法。An XmlWriter method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."
注解
此方法获取公共语言运行时对象,并使用 XML 架构定义语言 (XSD) 数据类型转换规则将输入值转换为所需的输出类型。This method takes the common language runtime object and converts the input value to the desired output type using the XML Schema definition language (XSD) data type conversion rules.
在调用 WriteValue 方法时,XmlWriter 根据 XML 架构 (XSD) 数据类型定义将值转换为其字符串表示形式并使用 WriteString 方法写出。When the WriteValue method is called, the XmlWriter converts the value to its string representations according to the XML Schema (XSD) data type rules and writes it out using the WriteString method.
如果继续多次调用 WriteValue,值不会通过空格分隔。If WriteValue is called multiple times in succession, the values are not delimited by a space. 必须在调用 WriteWhitespace 之间调用 WriteValue,以插入空白。You must call WriteWhitespace between calls to WriteValue to insert white space. 将数组类型传递给 XmlWriter.WriteValue(Object) 重载以写出非重复值的数组。Pass an array type to the XmlWriter.WriteValue(Object) overload to write out an array of distinct values.
如果在输出纯文本的实例上调用此方法,则将 XmlWriter 类型化值序列化为文本。If this method is called on an XmlWriter instance that outputs text-only, the typed value is serialized to text.
对于异步操作,请将此方法的返回值转换为字符串,并使用 WriteStringAsync 方法。For asynchronous operations, convert the return value of this method to a string and use the WriteStringAsync method.
适用于
WriteValue(Double)
public:
virtual void WriteValue(double value);
public virtual void WriteValue (double value);
abstract member WriteValue : double -> unit
override this.WriteValue : double -> unit
Public Overridable Sub WriteValue (value As Double)
参数
例外
指定了无效值。An invalid value was specified.
在上一次异步操作完成之前调用了 XmlWriter 方法。An XmlWriter method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."
示例
下面的示例使用类型化数据编写书节点。The following example writes a book node using typed data.
Double price = 9.95;
DateTime date = new DateTime(2004, 5, 20);
using (XmlWriter writer = XmlWriter.Create("data.xml"))
{
writer.WriteStartElement("book");
writer.WriteStartAttribute("pub-date");
writer.WriteValue(date);
writer.WriteEndAttribute();
writer.WriteStartElement("price");
writer.WriteValue(price);
writer.WriteEndElement();
writer.WriteEndElement();
writer.Flush();
}
Dim price As [Double] = 9.95
Dim [date] As New DateTime(2004, 5, 20)
Using writer As XmlWriter = XmlWriter.Create("data.xml")
writer.WriteStartElement("book")
writer.WriteStartAttribute("pub-date")
writer.WriteValue([date])
writer.WriteEndAttribute()
writer.WriteStartElement("price")
writer.WriteValue(price)
writer.WriteEndElement()
writer.WriteEndElement()
writer.Flush()
End Using
注解
此方法获取公共语言运行时对象,并使用 XML 架构定义语言 (XSD) 数据类型转换规则将输入值转换为所需的输出类型。This method takes the common language runtime object and converts the input value to the desired output type using the XML Schema definition language (XSD) data type conversion rules.
在调用 WriteValue 方法时,XmlWriter 根据 XML 架构 (XSD) 数据类型定义将值转换为其字符串表示形式并使用 WriteString 方法写出。When the WriteValue method is called, the XmlWriter converts the value to its string representations according to the XML Schema (XSD) data type rules and writes it out using the WriteString method.
如果继续多次调用 WriteValue,值不会通过空格分隔。If WriteValue is called multiple times in succession, the values are not delimited by a space. 必须在调用 WriteWhitespace 之间调用 WriteValue,以插入空白。You must call WriteWhitespace between calls to WriteValue to insert white space. 将数组类型传递给 XmlWriter.WriteValue(Object) 重载以写出非重复值的数组。Pass an array type to the XmlWriter.WriteValue(Object) overload to write out an array of distinct values.
如果在输出纯文本的实例上调用此方法,则将 XmlWriter 类型化值序列化为文本。If this method is called on an XmlWriter instance that outputs text-only, the typed value is serialized to text.
对于异步操作,请将此方法的返回值转换为字符串,并使用 WriteStringAsync 方法。For asynchronous operations, convert the return value of this method to a string and use the WriteStringAsync method.
适用于
WriteValue(String)
public:
virtual void WriteValue(System::String ^ value);
public virtual void WriteValue (string value);
public virtual void WriteValue (string? value);
abstract member WriteValue : string -> unit
override this.WriteValue : string -> unit
Public Overridable Sub WriteValue (value As String)
参数
例外
指定了无效值。An invalid value was specified.
在上一次异步操作完成之前调用了 XmlWriter 方法。An XmlWriter method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."
注解
此方法获取公共语言运行时对象,并使用 XML 架构定义语言 (XSD) 数据类型转换规则将输入值转换为所需的输出类型。This method takes the common language runtime object and converts the input value to the desired output type using the XML Schema definition language (XSD) data type conversion rules.
如果继续多次调用 WriteValue,值不会通过空格分隔。If WriteValue is called multiple times in succession, the values are not delimited by a space. 必须在调用 WriteWhitespace 之间调用 WriteValue,以插入空白。You must call WriteWhitespace between calls to WriteValue to insert white space. 将数组类型传递给 XmlWriter.WriteValue(Object) 重载以写出非重复值的数组。Pass an array type to the XmlWriter.WriteValue(Object) overload to write out an array of distinct values.
如果在输出纯文本的实例上调用此方法,则将 XmlWriter 类型化值序列化为文本。If this method is called on an XmlWriter instance that outputs text-only, the typed value is serialized to text.
对于异步操作,请使用 WriteStringAsync 方法。For asynchronous operations, use the WriteStringAsync method.
适用于
WriteValue(DateTimeOffset)
写入 DateTimeOffset 值。Writes a DateTimeOffset value.
public:
virtual void WriteValue(DateTimeOffset value);
public virtual void WriteValue (DateTimeOffset value);
abstract member WriteValue : DateTimeOffset -> unit
override this.WriteValue : DateTimeOffset -> unit
Public Overridable Sub WriteValue (value As DateTimeOffset)
参数
- value
- DateTimeOffset
要写入的 DateTimeOffset 值。The DateTimeOffset value to write.
例外
在上一次异步操作完成之前调用了 XmlWriter 方法。An XmlWriter method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."
注解
对于异步操作,请将此方法的返回值转换为字符串,并使用 WriteStringAsync 方法。For asynchronous operations, convert the return value of this method to a string and use the WriteStringAsync method.
适用于
WriteValue(DateTime)
public:
virtual void WriteValue(DateTime value);
public virtual void WriteValue (DateTime value);
abstract member WriteValue : DateTime -> unit
override this.WriteValue : DateTime -> unit
Public Overridable Sub WriteValue (value As DateTime)
参数
例外
指定了无效值。An invalid value was specified.
在上一次异步操作完成之前调用了 XmlWriter 方法。An XmlWriter method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."
示例
下面的示例使用类型化数据编写书节点。The following example writes a book node using typed data.
Double price = 9.95;
DateTime date = new DateTime(2004, 5, 20);
using (XmlWriter writer = XmlWriter.Create("data.xml"))
{
writer.WriteStartElement("book");
writer.WriteStartAttribute("pub-date");
writer.WriteValue(date);
writer.WriteEndAttribute();
writer.WriteStartElement("price");
writer.WriteValue(price);
writer.WriteEndElement();
writer.WriteEndElement();
writer.Flush();
}
Dim price As [Double] = 9.95
Dim [date] As New DateTime(2004, 5, 20)
Using writer As XmlWriter = XmlWriter.Create("data.xml")
writer.WriteStartElement("book")
writer.WriteStartAttribute("pub-date")
writer.WriteValue([date])
writer.WriteEndAttribute()
writer.WriteStartElement("price")
writer.WriteValue(price)
writer.WriteEndElement()
writer.WriteEndElement()
writer.Flush()
End Using
注解
此方法获取公共语言运行时对象,并使用 XML 架构定义语言 (XSD) 数据类型转换规则将输入值转换为所需的输出类型。This method takes the common language runtime object and converts the input value to the desired output type using the XML Schema definition language (XSD) data type conversion rules.
在调用 WriteValue 方法时,XmlWriter 根据 XML 架构 (XSD) 数据类型定义将值转换为其字符串表示形式并使用 WriteString 方法写出。When the WriteValue method is called, the XmlWriter converts the value to its string representations according to the XML Schema (XSD) data type rules and writes it out using the WriteString method.
如果继续多次调用 WriteValue,值不会通过空格分隔。If WriteValue is called multiple times in succession, the values are not delimited by a space. 必须在调用 WriteWhitespace 之间调用 WriteValue,以插入空白。You must call WriteWhitespace between calls to WriteValue to insert white space. 将数组类型传递给 XmlWriter.WriteValue(Object) 重载以写出非重复值的数组。Pass an array type to the XmlWriter.WriteValue(Object) overload to write out an array of distinct values.
如果在输出纯文本的实例上调用此方法,则将 XmlWriter 类型化值序列化为文本。If this method is called on an XmlWriter instance that outputs text-only, the typed value is serialized to text.
对于异步操作,请将此方法的返回值转换为字符串,并使用 WriteStringAsync 方法。For asynchronous operations, convert the return value of this method to a string and use the WriteStringAsync method.
适用于
WriteValue(Boolean)
public:
virtual void WriteValue(bool value);
public virtual void WriteValue (bool value);
abstract member WriteValue : bool -> unit
override this.WriteValue : bool -> unit
Public Overridable Sub WriteValue (value As Boolean)
参数
例外
指定了无效值。An invalid value was specified.
在上一次异步操作完成之前调用了 XmlWriter 方法。An XmlWriter method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."
注解
此方法获取公共语言运行时对象,并使用 XML 架构定义语言 (XSD) 数据类型转换规则将输入值转换为所需的输出类型。This method takes the common language runtime object and converts the input value to the desired output type using the XML Schema definition language (XSD) data type conversion rules.
在调用 WriteValue 方法时,XmlWriter 根据 XML 架构 (XSD) 数据类型定义将值转换为其字符串表示形式并使用 WriteString 方法写出。When the WriteValue method is called, the XmlWriter converts the value to its string representations according to the XML Schema (XSD) data type rules and writes it out using the WriteString method.
如果继续多次调用 WriteValue,值不会通过空格分隔。If WriteValue is called multiple times in succession, the values are not delimited by a space. 必须在调用 WriteWhitespace 之间调用 WriteValue,以插入空白。You must call WriteWhitespace between calls to WriteValue to insert white space. 将数组类型传递给 XmlWriter.WriteValue(Object) 重载以写出非重复值的数组。Pass an array type to the XmlWriter.WriteValue(Object) overload to write out an array of distinct values.
如果在输出纯文本的实例上调用此方法,则将 XmlWriter 类型化值序列化为文本。If this method is called on an XmlWriter instance that outputs text-only, the typed value is serialized to text.
对于异步操作,请将此方法的返回值转换为字符串,并使用 WriteStringAsync 方法。For asynchronous operations, convert the return value of this method to a string and use the WriteStringAsync method.
适用于
WriteValue(Decimal)
public:
virtual void WriteValue(System::Decimal value);
public virtual void WriteValue (decimal value);
abstract member WriteValue : decimal -> unit
override this.WriteValue : decimal -> unit
Public Overridable Sub WriteValue (value As Decimal)
参数
例外
指定了无效值。An invalid value was specified.
在上一次异步操作完成之前调用了 XmlWriter 方法。An XmlWriter method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."
注解
此方法获取公共语言运行时对象,并使用 XML 架构定义语言 (XSD) 数据类型转换规则将输入值转换为所需的输出类型。This method takes the common language runtime object and converts the input value to the desired output type using the XML Schema definition language (XSD) data type conversion rules.
在调用 WriteValue 方法时,XmlWriter 根据 XML 架构 (XSD) 数据类型定义将值转换为其字符串表示形式并使用 WriteString 方法写出。When the WriteValue method is called, the XmlWriter converts the value to its string representations according to the XML Schema (XSD) data type rules and writes it out using the WriteString method.
如果继续多次调用 WriteValue,值不会通过空格分隔。If WriteValue is called multiple times in succession, the values are not delimited by a space. 必须在调用 WriteWhitespace 之间调用 WriteValue,以插入空白。You must call WriteWhitespace between calls to WriteValue to insert white space. 将数组类型传递给 XmlWriter.WriteValue(Object) 重载以写出非重复值的数组。Pass an array type to the XmlWriter.WriteValue(Object) overload to write out an array of distinct values.
如果在输出纯文本的实例上调用此方法,则将 XmlWriter 类型化值序列化为文本。If this method is called on an XmlWriter instance that outputs text-only, the typed value is serialized to text.
对于异步操作,请将此方法的返回值转换为字符串,并使用 WriteStringAsync 方法。For asynchronous operations, convert the return value of this method to a string and use the WriteStringAsync method.