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.

C#
public virtual void WriteValue(float value);

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

.NET 10 and other versions
Product Versions
.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, 10
.NET Framework 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

WriteValue(Object)

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

Writes the object value.

C#
public virtual void WriteValue(object value);

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

.NET 10 and other versions
Product Versions
.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, 10
.NET Framework 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

WriteValue(Int64)

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

Writes a Int64 value.

C#
public virtual void WriteValue(long value);

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

.NET 10 and other versions
Product Versions
.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, 10
.NET Framework 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

WriteValue(Int32)

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

Writes a Int32 value.

C#
public virtual void WriteValue(int value);

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

.NET 10 and other versions
Product Versions
.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, 10
.NET Framework 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

WriteValue(Double)

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

Writes a Double value.

C#
public virtual void WriteValue(double value);

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.

C#
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();
}

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

.NET 10 and other versions
Product Versions
.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, 10
.NET Framework 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

WriteValue(String)

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

Writes a String value.

C#
public virtual void WriteValue(string value);
C#
public virtual void WriteValue(string? value);

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

.NET 10 and other versions
Product Versions
.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, 10
.NET Framework 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

WriteValue(DateTimeOffset)

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

Writes a DateTimeOffset value.

C#
public virtual void WriteValue(DateTimeOffset value);

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

.NET 10 and other versions
Product Versions
.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, 10
.NET Framework 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

WriteValue(DateTime)

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

Writes a DateTime value.

C#
public virtual void WriteValue(DateTime value);

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.

C#
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();
}

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

.NET 10 and other versions
Product Versions
.NET Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 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.3, 1.4, 1.6, 2.0, 2.1

WriteValue(Boolean)

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

Writes a Boolean value.

C#
public virtual void WriteValue(bool value);

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

.NET 10 and other versions
Product Versions
.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, 10
.NET Framework 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

WriteValue(Decimal)

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

Writes a Decimal value.

C#
public virtual void WriteValue(decimal value);

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

.NET 10 and other versions
Product Versions
.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, 10
.NET Framework 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