語言

XmlConvert.ToDateTimeOffset 方法

定義

將所供應 String 的轉換成 DateTimeOffset 等效的。

多載

名稱 Description
ToDateTimeOffset(String, String[])

將所供應 String 的轉換成 DateTimeOffset 等效的。

ToDateTimeOffset(String, String)

將所供應 String 的轉換成 DateTimeOffset 等效的。

ToDateTimeOffset(String)

將所供應 String 的轉換成 DateTimeOffset 等效的。

ToDateTimeOffset(String, String[])

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
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[]

一組 .NET 標準或自訂日期與時間格式字串陣列,指定允許的s格式。

傳回

相當 DateTimeOffset 於提供的弦。

例外狀況

在不變文化規則下,元素 formats 是無效的,或 s 與不 formats 完全符合的元素,除了前置或後置的空白外。

範例

以下範例示範如何從 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.ParseExact(String, String[], IFormatProvider, DateTimeStyles) 和 DateTimeFormatInfo.InvariantInfoAllowLeadingWhiteAllowTrailingWhite 和 旗標。 前置與後方的空白空間會被忽略,但 s 其他部分必須與 中 formats某一串字串完全匹配。 該formats參數使用 .NET 標準日期與時間格式字串或自訂日期與時間格式字串,而非 XML Schema 的日期時間模式。

在自訂格式字串中, z、 zz、 和 zzz specifier 需要有符號的數值 UTC 偏移量。 它們與 XML UTC 編號 Z不符。 當s可以包含 Z時,請使用K自訂格式指定器。

如果輸入字串中指定的偏移量會導致 DateTimeOffset 的反序列化表示產生溢位,則會拋出 FormatException。

當分秒數中指定超過七位數時,該數值會四捨五入。 例如,00000004變成000000,00000005變成0000001。

適用於

ToDateTimeOffset(String, String)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
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

一個 .NET 標準或自訂的日期與時間格式字串,指定所需的格式。s

傳回

相當 DateTimeOffset 於提供的弦。

例外狀況

s 是 null。

format 在不變文化規則下,或 s 在不變文化規則下並不完全吻合 format ,除了前置或後置的空白空間外。

範例

以下範例示範如何從 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.ParseExact(String, String, IFormatProvider, DateTimeStyles) 和 DateTimeFormatInfo.InvariantInfoAllowLeadingWhiteAllowTrailingWhite 和 旗標。 前置與後置空白會被忽略,但 s 其他部分必須完全匹配 format。 該format參數使用 .NET 標準日期與時間格式字串或自訂日期與時間格式字串,而非 XML Schema 的日期時間模式。

在自訂格式字串中, z、 zz、 和 zzz specifier 需要有符號的數值 UTC 偏移量。 它們與 XML UTC 編號 Z不符。 當s可以包含 Z時,請使用K自訂格式指定器。

如果輸入字串中指定的偏移量會導致 DateTimeOffset 的反序列化表示產生溢位,則會拋出 FormatException。

當分秒數中指定超過七位數時,該數值會四捨五入。 例如,00000004變成000000,00000005變成0000001。

適用於

ToDateTimeOffset(String)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
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

要轉換的字串。 字串必須符合 W3C 建議中 XML dateTime 類型的子集。 欲了解更多資訊,請參閱 XML 架構規範中的 dateTime 章節。

傳回

相當 DateTimeOffset 於提供的弦。

例外狀況

s 是 null。

傳遞給此方法的參數超出允許值範圍。 關於允許值的資訊,請參見 DateTimeOffset。

傳遞給此方法的參數不符合 W3C 建議中關於 XML dateTime 類型的子集。 欲了解更多資訊,請參閱 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變成000000,00000005變成0000001。

適用於