次の方法で共有


XmlConvert.ToDateTimeOffset Method (String, array<String[])

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts the supplied String to a DateTimeOffset equivalent.

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

Syntax

'Declaration
Public Shared Function ToDateTimeOffset ( _
    s As String, _
    formats As String() _
) As DateTimeOffset
public static DateTimeOffset ToDateTimeOffset(
    string s,
    string[] formats
)

Parameters

  • formats
    Type: array<System.String[]
    An array of formats from which s can be converted. Each format in formats can be any subset of the W3C Recommendation for the XML dateTime type. For more information see XML Schema Part 2: Datatypes. The string s is validated against one of these formats.

Return Value

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

Remarks

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.

Examples

Dim xmlString As String = _
    "<?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>"

Dim output As New StringBuilder()

' Create an XmlReader
Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString))
    reader.ReadToFollowing("time")
    Dim time As String = reader.ReadElementContentAsString()

    ' Specify formats against which time will be validated before conversion to DateTimeOffset
    ' If time does not match one of the specified formats, a FormatException will be thrown.
    ' Each specified format must be a subset of the W3C Recommendation for the XML dateTime type
    Dim formats As String() = {"yyyy-MM-ddTHH:mm:sszzzzzzz", "yyyy-MM-ddTHH:mm:ss", "yyyy-MM-dd"}
    Try
        ' Read the element contents as a string and covert to DateTimeOffset type
        Dim transaction_time As DateTimeOffset = XmlConvert.ToDateTimeOffset(time, formats)
        output.AppendLine(transaction_time.ToString())
    Catch e As Exception
        output.Append(e)
    End Try
End Using

' Display the output to the TextBlock control
OutputTextBlock.Text = output.ToString()

String xmlString =
    @"<?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>";

StringBuilder output = new StringBuilder();

// Create an XmlReader
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
    reader.ReadToFollowing("time");
    string time = reader.ReadElementContentAsString();

    // Specify formats against which time will be validated before conversion to DateTimeOffset
    // If time does not match one of the specified formats, a FormatException will be thrown.
    // Each specified format must be a subset of the W3C Recommendation for the XML dateTime type
    string[] formats = { "yyyy-MM-ddTHH:mm:sszzzzzzz", "yyyy-MM-ddTHH:mm:ss", "yyyy-MM-dd" };
    try
    {
        // Read the element contents as a string and covert to DateTimeOffset type
        DateTimeOffset transaction_time = XmlConvert.ToDateTimeOffset(time, formats);
        output.AppendLine(transaction_time.ToString());
    }
    catch (Exception e)
    {
        output.Append(e);
    }
}
// Display the output to the TextBlock control
OutputTextBlock.Text = output.ToString();

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.