Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

XmlConvert.ToDateTimeOffset Method (String, 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,
	string format
)

Parameters

s
Type: System.String

The string to convert.

format
Type: System.String

The format from which s is converted. The format parameter can be any subset of the W3C Recommendation for the XML dateTime type. (For more information see http://www.w3.org/TR/xmlschema-2/#dateTime.) The string s is validated against this format.

Return Value

Type: System.DateTimeOffset
The DateTimeOffset equivalent of the supplied string.
ExceptionCondition
ArgumentNullException

s is null.

FormatException

s or format is an empty string or is not in the specified format.

If the offset specified within the input string will cause an overflow in the deserialized representation of the DateTimeOffset, a FormatException is thrown.

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. The input string is validated against the specified format before being converted.

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");
        reader.ReadToFollowing("time");
        string time = reader.ReadElementContentAsString();

        // Specify a format against which time will be validated before conversion to DateTimeOffset 
        // If time does not match the format, a FormatException will be thrown. 
        // The specified format must be a subset of the W3C Recommendation for the XML dateTime type
        string format = "yyyy-MM-ddTHH:mm:sszzzzzzz";
        try
        {
            // Read the element contents as a string and covert to DateTimeOffset type
            DateTimeOffset transaction_time = XmlConvert.ToDateTimeOffset(time, format);
            Console.WriteLine(transaction_time);
        }
        catch(Exception e)
        {
            Console.WriteLine(e);
        }
    }
}

The example uses the transactions.xml file.

<?xml version="1.0"?>
<transactions>
   <transaction>
      <id>123456789</id>
      <amount>1.00</amount>
      <currency>USD</currency>
      <time>2007-08-03T22:05:13-07:00</time>
   </transaction>
</transactions>

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1, 3.0 SP1, 2.0 SP1
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.