Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

XmlConvert.ToDateTimeOffset Method (String)


Converts the supplied String to a DateTimeOffset equivalent.

Namespace:   System.Xml
Assembly:  System.Xml (in System.Xml.dll)

public static DateTimeOffset ToDateTimeOffset(
	string s


Type: System.String

The string to convert.

Note   The string must conform to a subset of the W3C Recommendation for the XML dateTime type. For more information see

Return Value

Type: System.DateTimeOffset

The DateTimeOffset equivalent of the supplied string.

Exception Condition

s is null.


The argument passed to this method is outside the range of allowable values. For information about allowable values, see DateTimeOffset.


The argument passed to this method does not conform to a subset of the W3C Recommendations for the XML dateTime type. For more information see

When more than seven digits are specified for fractional seconds, the value is rounded. For example, 00000004 becomes 0000000 and 00000005 becomes 0000001.

The following example demonstrates how to read a string from an XML file and use the ToDateTimeOffset method to convert the string to a DateTimeOffset type.

using System;
using System.Xml;

class Example
    static void Main()
        // Create an XmlReader, read to the "time" element, and read contents as type string
        XmlReader reader = XmlReader.Create("transactions.xml");
        string time = reader.ReadElementContentAsString();

        // Read the element contents as a string and covert to DateTimeOffset type
        // The format of time must be a subset of the W3C Recommendation for the XML dateTime type
        DateTimeOffset transaction_time = XmlConvert.ToDateTimeOffset(time);

The example uses the transactions.xml file.

<?xml version="1.0"?>

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
© 2017 Microsoft