XmlConvert.ToDateTimeOffset 方法

定義

將提供的 String 轉換成 DateTimeOffset 對等用法。

多載

ToDateTimeOffset(String, String[])

將提供的 String 轉換成 DateTimeOffset 對等用法。

ToDateTimeOffset(String, String)

將提供的 String 轉換成 DateTimeOffset 對等用法。

ToDateTimeOffset(String)

將提供的 String 轉換成 DateTimeOffset 對等用法。

ToDateTimeOffset(String, String[])

Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs

將提供的 String 轉換成 DateTimeOffset 對等用法。

public:
 static DateTimeOffset ToDateTimeOffset(System::String ^ s, cli::array <System::String ^> ^ formats);
public static DateTimeOffset ToDateTimeOffset (string s, string[] formats);
static member ToDateTimeOffset : string * string[] -> DateTimeOffset
Public Shared Function ToDateTimeOffset (s As String, formats As String()) As DateTimeOffset

參數

s
String

要轉換的字串。

formats
String[]

轉換 s 之來源格式的陣列。 formats 中的每個格式,可以是 XML dateTime 型別的 W3C Recommendation 子集 (如需詳細資訊,請參閱 XML 結構描述規格的 dateTime 一節。)字串 s 會針對其中一種格式進行驗證。

傳回

所提供之字串的 DateTimeOffset 對應項。

範例

下列範例示範如何從 XML 檔案讀取字串,並使用 ToDateTimeOffset 方法將字串 DateTimeOffset 轉換成類型。 輸入字串必須先根據其中一個指定的格式進行驗證,才能進行轉換。

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 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);
            Console.WriteLine(transaction_time);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
}
Imports System.Xml

Module Module1
    Sub Main()
        ' Create an XmlReader, read to the "time" element, and read contents as type string
        Dim reader As XmlReader = XmlReader.Create("transactions.xml")
        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)
            Console.WriteLine(transaction_time)
        Catch e As Exception
            Console.WriteLine(e)
        End Try
    End Sub
End Module

此範例會使用 transactions.xml 檔案。

<?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>

備註

如果輸入字串內指定的位移會導致 DateTimeOffset 的還原序列化標記法溢位,則會擲回 FormatException。

在小數秒指定超過七位數時,值會四捨五入。 例如,00000004會變成 00000000,而00000005會變成0000001。

適用於

ToDateTimeOffset(String, String)

Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs

將提供的 String 轉換成 DateTimeOffset 對等用法。

public:
 static DateTimeOffset ToDateTimeOffset(System::String ^ s, System::String ^ format);
public static DateTimeOffset ToDateTimeOffset (string s, string format);
static member ToDateTimeOffset : string * string -> DateTimeOffset
Public Shared Function ToDateTimeOffset (s As String, format As String) As DateTimeOffset

參數

s
String

要轉換的字串。

format
String

轉換 s 的來源格式。 格式參數可以是 XML dateTime 型別之 W3C Recommendation 的任何子集 (如需詳細資訊,請參閱 XML 結構描述規格的 dateTime 一節。)字串 s 會針對這個格式進行驗證。

傳回

所提供之字串的 DateTimeOffset 對應項。

例外狀況

snull

sformat 是空字串或沒有使用指定的格式。

範例

下列範例示範如何從 XML 檔案讀取字串,並使用 ToDateTimeOffset 方法將字串 DateTimeOffset 轉換成類型。 在轉換之前,輸入字串會根據指定的格式進行驗證。

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);
        }
    }
}
Imports System.Xml

Module Module1      
    Sub Main()
        ' Create an XmlReader, read to the "time" element, and read contents as type string
        Dim reader As XmlReader = XmlReader.Create("transactions.xml")
        reader.ReadToFollowing("time")
        Dim time As String = 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
        Dim format As String = "yyyy-MM-ddTHH:mm:sszzzzzzz"
        Try
            ' Read the element contents as a string and covert to DateTimeOffset type
            Dim transaction_time As DateTimeOffset = XmlConvert.ToDateTimeOffset(time, format)
            Console.WriteLine(transaction_time)
        Catch e As Exception
            Console.WriteLine(e)
        End Try
    End Sub
End Module

此範例會使用 transactions.xml 檔案。

<?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>

備註

如果輸入字串內指定的位移會導致 DateTimeOffset 的還原序列化標記法溢位,則會擲回 FormatException。

在小數秒指定超過七位數時,值會四捨五入。 例如,00000004會變成 00000000,而00000005會變成0000001。

適用於

ToDateTimeOffset(String)

Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs

將提供的 String 轉換成 DateTimeOffset 對等用法。

public:
 static DateTimeOffset ToDateTimeOffset(System::String ^ s);
public static DateTimeOffset ToDateTimeOffset (string s);
static member ToDateTimeOffset : string -> DateTimeOffset
Public Shared Function ToDateTimeOffset (s As String) As DateTimeOffset

參數

s
String

要轉換的字串。 字串必須符合 XML dateTime 類型的 W3C 建議子集。 如需詳細資訊,請參閱 XML 結構描述規格的 dateTime 一節。

傳回

所提供之字串的 DateTimeOffset 對應項。

例外狀況

snull

傳遞至這個方法的引數是在容許值的範圍之外。 如需有關容許值的詳細資訊,請參閱 DateTimeOffset

傳遞至這個方法的引數不符合 XML dateTime 型別的 W3C Recommendations 子集。 如需詳細資訊,請參閱 XML 結構描述規格的 dateTime 一節。

範例

下列範例示範如何從 XML 檔案讀取字串,並使用 ToDateTimeOffset 方法將字串 DateTimeOffset 轉換成類型。

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();

        // 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);
        Console.WriteLine(transaction_time);
    }
}
Imports System.Xml

Module Module1
    Sub Main()
        ' Create an XmlReader, read to the "time" element, and read contents as type string
        Dim reader As XmlReader = XmlReader.Create("transactions.xml")
        reader.ReadToFollowing("time")
        Dim time As String = 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
        Dim transaction_time As DateTimeOffset = XmlConvert.ToDateTimeOffset(time)
        Console.WriteLine(transaction_time)
    End Sub
End Module

此範例會使用 transactions.xml 檔案。

<?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>

備註

在小數秒指定超過七位數時,值會四捨五入。 例如,00000004會變成 00000000,而00000005會變成0000001。

適用於