XmlConvert.ToDateTimeOffset 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將所供應 String 的轉換成 DateTimeOffset 等效的。
多載
| 名稱 | Description |
|---|---|
| ToDateTimeOffset(String, String[]) |
將所供應 String 的轉換成 DateTimeOffset 等效的。 |
| ToDateTimeOffset(String, String) |
將所供應 String 的轉換成 DateTimeOffset 等效的。 |
| ToDateTimeOffset(String) |
將所供應 String 的轉換成 DateTimeOffset 等效的。 |
ToDateTimeOffset(String, String[])
將所供應 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 都可以是 W3C 建議中 XML dateTime 類型的任一子集。 (更多資訊請參閱 XML Schema 規範中的 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變成000000,00000005變成0000001。
適用於
ToDateTimeOffset(String, String)
將所供應 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 轉換而來的。 格式參數可以是 W3C 建議中 XML dateTime 類型的任何子集。 (更多資訊請參閱 XML Schema 規範中的 dateTime 章節。)字串 s 會依照此格式進行驗證。
傳回
相當 DateTimeOffset 於提供的弦。
例外狀況
s 是 null。
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 的反序列化表示產生溢位,則會拋出 FormatException。
當分秒數中指定超過七位數時,該數值會四捨五入。 例如,00000004變成000000,00000005變成0000001。
適用於
ToDateTimeOffset(String)
將所供應 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
參數
傳回
相當 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。