Upravit

Sdílet prostřednictvím


XmlWriter.WriteValue Method

Definition

Writes a single simple-typed value.

Overloads

WriteValue(Single)

Writes a single-precision floating-point number.

WriteValue(Object)

Writes the object value.

WriteValue(Int64)

Writes a Int64 value.

WriteValue(Int32)

Writes a Int32 value.

WriteValue(Double)

Writes a Double value.

WriteValue(String)

Writes a String value.

WriteValue(DateTimeOffset)

Writes a DateTimeOffset value.

WriteValue(DateTime)

Writes a DateTime value.

WriteValue(Boolean)

Writes a Boolean value.

WriteValue(Decimal)

Writes a Decimal value.

Remarks

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. This enables you to pipeline simple-type values with minimal reparsing overhead when passing data between XPathDocument, XmlReader, and XmlWriter objects. This ability is also useful when dealing with CLR simple-types and an XmlWriter instance. 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.

For asynchronous operations, convert the return value of WriteValue to a string and use the WriteStringAsync method.

The following table shows the default XSD data types that correspond to CLR types.

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 type Default XSD data type
System.Boolean xsd:boolean
System.Byte** xsd:integer
System.Byte array xsd:base64Binary
System.Char** xsd:string
System.DateTime xsd:dateTime
System.Decimal xsd:decimal
System.Double xsd:double
System.Int16** xsd:integer
System.Int32 xsd:integer
System.Int64 xsd:integer
System.Single xsd:float
System.String xsd:string
System.IO.TextReader xsd:string
System.IO.BinaryReader xsd:base64Binary

**These types are not CLS-compliant. They do not have corresponding WriteValue methods.

If WriteValue is called multiple times in succession, the values are not delimited by a space. You must call WriteWhitespace between calls to WriteValue to insert white space.

WriteValue(Single)

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

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)

Parameters

value
Single

The single-precision floating-point number to write.

Exceptions

An invalid value was specified.

An XmlWriter method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

Remarks

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.

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.

If WriteValue is called multiple times in succession, the values are not delimited by a space. You must call WriteWhitespace between calls to WriteValue to insert white space. Pass an array type to the XmlWriter.WriteValue(Object) overload to write out an array of distinct values.

If this method is called on an XmlWriter instance that outputs text-only, the typed value is serialized to text.

For asynchronous operations, convert the return value of this method to a string and use the WriteStringAsync method.

Applies to

WriteValue(Object)

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

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)

Parameters

value
Object

The object value to write.

Note With the release of the .NET Framework 3.5, this method accepts DateTimeOffset as a parameter.

Exceptions

An invalid value was specified.

The value is null.

The writer is closed or in error state.

-or-

An XmlWriter method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

Remarks

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.

If the CLR object is a list type such as IEnumerable, IList, or ICollection, it is treated as an array of the value type. 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.

If this method is called on an XmlWriter instance that outputs text-only, the typed value is serialized to text.

For asynchronous operations, convert the return value of this method to a string and use the WriteStringAsync method.

Applies to

WriteValue(Int64)

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

Writes a Int64 value.

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)

Parameters

value
Int64

The Int64 value to write.

Exceptions

An invalid value was specified.

An XmlWriter method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

Remarks

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.

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.

If WriteValue is called multiple times in succession, the values are not delimited by a space. You must call WriteWhitespace between calls to WriteValue to insert white space. Pass an array type to the XmlWriter.WriteValue(Object) overload to write out an array of distinct values.

If this method is called on an XmlWriter instance that outputs text-only, the typed value is serialized to text.

For asynchronous operations, convert the return value of this method to a string and use the WriteStringAsync method.

Applies to

WriteValue(Int32)

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

Writes a Int32 value.

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)

Parameters

value
Int32

The Int32 value to write.

Exceptions

An invalid value was specified.

An XmlWriter method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

Remarks

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.

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.

If WriteValue is called multiple times in succession, the values are not delimited by a space. You must call WriteWhitespace between calls to WriteValue to insert white space. Pass an array type to the XmlWriter.WriteValue(Object) overload to write out an array of distinct values.

If this method is called on an XmlWriter instance that outputs text-only, the typed value is serialized to text.

For asynchronous operations, convert the return value of this method to a string and use the WriteStringAsync method.

Applies to

WriteValue(Double)

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

Writes a Double value.

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)

Parameters

value
Double

The Double value to write.

Exceptions

An invalid value was specified.

An XmlWriter method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

Examples

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

Remarks

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.

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.

If WriteValue is called multiple times in succession, the values are not delimited by a space. You must call WriteWhitespace between calls to WriteValue to insert white space. Pass an array type to the XmlWriter.WriteValue(Object) overload to write out an array of distinct values.

If this method is called on an XmlWriter instance that outputs text-only, the typed value is serialized to text.

For asynchronous operations, convert the return value of this method to a string and use the WriteStringAsync method.

Applies to

WriteValue(String)

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

Writes a String value.

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)

Parameters

value
String

The String value to write.

Exceptions

An invalid value was specified.

An XmlWriter method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

Remarks

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.

If WriteValue is called multiple times in succession, the values are not delimited by a space. You must call WriteWhitespace between calls to WriteValue to insert white space. Pass an array type to the XmlWriter.WriteValue(Object) overload to write out an array of distinct values.

If this method is called on an XmlWriter instance that outputs text-only, the typed value is serialized to text.

For asynchronous operations, use the WriteStringAsync method.

Applies to

WriteValue(DateTimeOffset)

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

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)

Parameters

value
DateTimeOffset

The DateTimeOffset value to write.

Exceptions

An XmlWriter method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

Remarks

For asynchronous operations, convert the return value of this method to a string and use the WriteStringAsync method.

Applies to

WriteValue(DateTime)

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

Writes a DateTime value.

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)

Parameters

value
DateTime

The DateTime value to write.

Exceptions

An invalid value was specified.

An XmlWriter method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

Examples

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

Remarks

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.

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.

If WriteValue is called multiple times in succession, the values are not delimited by a space. You must call WriteWhitespace between calls to WriteValue to insert white space. Pass an array type to the XmlWriter.WriteValue(Object) overload to write out an array of distinct values.

If this method is called on an XmlWriter instance that outputs text-only, the typed value is serialized to text.

For asynchronous operations, convert the return value of this method to a string and use the WriteStringAsync method.

Applies to

WriteValue(Boolean)

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

Writes a Boolean value.

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)

Parameters

value
Boolean

The Boolean value to write.

Exceptions

An invalid value was specified.

An XmlWriter method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

Remarks

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.

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.

If WriteValue is called multiple times in succession, the values are not delimited by a space. You must call WriteWhitespace between calls to WriteValue to insert white space. Pass an array type to the XmlWriter.WriteValue(Object) overload to write out an array of distinct values.

If this method is called on an XmlWriter instance that outputs text-only, the typed value is serialized to text.

For asynchronous operations, convert the return value of this method to a string and use the WriteStringAsync method.

Applies to

WriteValue(Decimal)

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

Writes a Decimal value.

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)

Parameters

value
Decimal

The Decimal value to write.

Exceptions

An invalid value was specified.

An XmlWriter method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

Remarks

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.

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.

If WriteValue is called multiple times in succession, the values are not delimited by a space. You must call WriteWhitespace between calls to WriteValue to insert white space. Pass an array type to the XmlWriter.WriteValue(Object) overload to write out an array of distinct values.

If this method is called on an XmlWriter instance that outputs text-only, the typed value is serialized to text.

For asynchronous operations, convert the return value of this method to a string and use the WriteStringAsync method.

Applies to