XmlConvert.ToDateTimeOffset Metoda

Definicja

Konwertuje dostarczony String element DateTimeOffset na odpowiednik.

Przeciążenia

ToDateTimeOffset(String, String[])

Konwertuje dostarczony String element DateTimeOffset na odpowiednik.

ToDateTimeOffset(String, String)

Konwertuje dostarczony String element DateTimeOffset na odpowiednik.

ToDateTimeOffset(String)

Konwertuje dostarczony String element DateTimeOffset na odpowiednik.

ToDateTimeOffset(String, String[])

Konwertuje dostarczony String element DateTimeOffset na odpowiednik.

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

Parametry

s
String

Ciąg do konwersji.

formats
String[]

Tablica formatów, z których s można przekonwertować. Każdy format w formats programie może być dowolnym podzbiorem zalecenia W3C dla typu data/godzina XML. (Aby uzyskać więcej informacji, zobacz sekcję dateTime specyfikacji schematu XML). Ciąg s jest weryfikowany w jednym z tych formatów.

Zwraca

DateTimeOffset

Odpowiednik DateTimeOffset podanego ciągu.

Przykłady

W poniższym przykładzie pokazano, jak odczytać ciąg z pliku XML i użyć ToDateTimeOffset metody w celu przekonwertowania ciągu na DateTimeOffset typ. Przed przekonwertowaniem ciąg wejściowy musi zostać zweryfikowany pod kątem jednego z określonych formatów.

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

W przykładzie użyto pliku 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>  

Uwagi

Jeśli przesunięcie określone w ciągu wejściowym spowoduje przepełnienie w deserializowanej reprezentacji elementu DateTimeOffset, zostanie zgłoszony wyjątek FormatException.

Gdy dla sekund ułamkowych określono więcej niż siedem cyfr, wartość jest zaokrąglona. Na przykład 00000004 staje się 0000000, a 00000005 staje się 0000001.

Dotyczy

ToDateTimeOffset(String, String)

Konwertuje dostarczony String element DateTimeOffset na odpowiednik.

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

Parametry

s
String

Ciąg do konwersji.

format
String

Format, z którego s jest konwertowany. Parametr formatu może być dowolnym podzbiorem zalecenia W3C dla typu data/godzina XML. (Aby uzyskać więcej informacji, zobacz sekcję dateTime specyfikacji schematu XML). Ciąg s jest weryfikowany w tym formacie.

Zwraca

DateTimeOffset

Odpowiednik DateTimeOffset podanego ciągu.

Wyjątki

s lub format jest pustym ciągiem lub nie ma określonego formatu.

Przykłady

W poniższym przykładzie pokazano, jak odczytać ciąg z pliku XML i użyć ToDateTimeOffset metody w celu przekonwertowania ciągu na DateTimeOffset typ. Ciąg wejściowy jest weryfikowany pod kątem określonego formatu przed przekonwertowaniem.

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

W przykładzie użyto pliku 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>  

Uwagi

Jeśli przesunięcie określone w ciągu wejściowym spowoduje przepełnienie w deserializowanej reprezentacji elementu DateTimeOffset, zostanie zgłoszony wyjątek FormatException.

Gdy dla sekund ułamkowych określono więcej niż siedem cyfr, wartość jest zaokrąglona. Na przykład 00000004 staje się 0000000, a 00000005 staje się 0000001.

Dotyczy

ToDateTimeOffset(String)

Konwertuje dostarczony String element DateTimeOffset na odpowiednik.

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

Parametry

s
String

Ciąg do konwersji. Ciąg musi być zgodny z podzbiorem zalecenia W3C dla typu data/godzina XML. Aby uzyskać więcej informacji, zobacz sekcję dateTime specyfikacji schematu XML.

Zwraca

DateTimeOffset

Odpowiednik DateTimeOffset podanego ciągu.

Wyjątki

Argument przekazany do tej metody znajduje się poza zakresem dopuszczalnych wartości. Aby uzyskać informacje o dozwolonych wartościach, zobacz DateTimeOffset.

Argument przekazany do tej metody nie jest zgodny z podzbiorem Rekomendacje W3C dla typu data/godzina XML. Aby uzyskać więcej informacji, zobacz sekcję dateTime specyfikacji schematu XML.

Przykłady

W poniższym przykładzie pokazano, jak odczytać ciąg z pliku XML i użyć ToDateTimeOffset metody w celu przekonwertowania ciągu na DateTimeOffset typ.

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

W przykładzie użyto pliku 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>  

Uwagi

Gdy dla sekund ułamkowych określono więcej niż siedem cyfr, wartość jest zaokrąglona. Na przykład 00000004 staje się 0000000, a 00000005 staje się 0000001.

Dotyczy