Język

XmlConvert.ToDateTimeOffset Metoda

Definicja

Konwertuje dostarczony String element na DateTimeOffset odpowiednik.

Przeciążenia

Nazwa Opis
ToDateTimeOffset(String, String[])

Konwertuje dostarczony String element na DateTimeOffset odpowiednik.

ToDateTimeOffset(String, String)

Konwertuje dostarczony String element na DateTimeOffset odpowiednik.

ToDateTimeOffset(String)

Konwertuje dostarczony String element na DateTimeOffset odpowiednik.

ToDateTimeOffset(String, String[])

Źródło:
XmlConvert.cs
Źródło:
XmlConvert.cs
Źródło:
XmlConvert.cs
Źródło:
XmlConvert.cs
Źródło:
XmlConvert.cs

Konwertuje dostarczony String element na DateTimeOffset 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, który ma zostać przekonwertowany.

formats
String[]

Tablica .NET standardowych lub niestandardowych ciągów formatu daty i godziny, które określają dozwolone formaty s.

Zwraca

Odpowiednik DateTimeOffset podanego ciągu.

Wyjątki

Element elementu formats jest nieprawidłowy lub s nie pasuje dokładnie do żadnego elementu formats w ramach niezmiennych reguł kultury, z wyjątkiem wiodących lub końcowych białych znaków.

Przykłady

W poniższym przykładzie pokazano, jak odczytać ciąg z pliku XML i użyć ToDateTimeOffset metody , aby przekonwertować ciąg na DateTimeOffset typ. Ciąg wejściowy musi zostać zweryfikowany względem jednego z określonych formatów 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 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

To przeciążenie jest równoważne wywołaniu DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles) z flagami DateTimeFormatInfo.InvariantInfo i i AllowLeadingWhiteAllowTrailingWhite . Białe znaki wiodące i końcowe są ignorowane, ale s w przeciwnym razie muszą dokładnie odpowiadać jednemu z ciągów w pliku formats. Parametr formats używa .NET standardowych ciągów formatu daty i godziny lub niestandardowych ciągów formatudaty i godziny, a nie wzorców daty/godziny schematu XML.

W ciągu zformatu niestandardowego specyfikatory , zzi zzz wymagają podpisanego przesunięcia numerycznego UTC. Nie są one zgodne z projektantem ZXML UTC . Użyj specyfikatora formatu niestandardowego K , jeśli s może zawierać Zwartość .

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.

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

Dotyczy

ToDateTimeOffset(String, String)

Źródło:
XmlConvert.cs
Źródło:
XmlConvert.cs
Źródło:
XmlConvert.cs
Źródło:
XmlConvert.cs
Źródło:
XmlConvert.cs

Konwertuje dostarczony String element na DateTimeOffset 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, który ma zostać przekonwertowany.

format
String

Standardowy lub niestandardowy ciąg formatu daty i godziny .NET określający wymagany format s.

Zwraca

Odpowiednik DateTimeOffset podanego ciągu.

Wyjątki

Parametr s ma wartość null.

format jest nieprawidłowy lub s nie pasuje format dokładnie do niezmiennych reguł kultury, z wyjątkiem wiodącego lub końcowego odstępu.

Przykłady

W poniższym przykładzie pokazano, jak odczytać ciąg z pliku XML i użyć ToDateTimeOffset metody , aby przekonwertować ciąg na DateTimeOffset typ. Ciąg wejściowy jest weryfikowany względem 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

To przeciążenie jest równoważne wywołaniu DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles) z flagami DateTimeFormatInfo.InvariantInfo i i AllowLeadingWhiteAllowTrailingWhite . Białe znaki wiodące i końcowe są ignorowane, ale s w przeciwnym razie muszą dokładnie odpowiadać formatwartości . Parametr format używa .NET standardowych ciągów formatu daty i godziny lub niestandardowych ciągów formatudaty i godziny, a nie wzorców daty/godziny schematu XML.

W ciągu zformatu niestandardowego specyfikatory , zzi zzz wymagają podpisanego przesunięcia numerycznego UTC. Nie są one zgodne z projektantem ZXML UTC . Użyj specyfikatora formatu niestandardowego K , jeśli s może zawierać Zwartość .

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.

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

Dotyczy

ToDateTimeOffset(String)

Źródło:
XmlConvert.cs
Źródło:
XmlConvert.cs
Źródło:
XmlConvert.cs
Źródło:
XmlConvert.cs
Źródło:
XmlConvert.cs

Konwertuje dostarczony String element na DateTimeOffset 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, który ma zostać przekonwertowany. Ciąg musi być zgodny z podzbiorem rekomendacji W3C dla typu data/godzina XML. Aby uzyskać więcej informacji, zobacz sekcję dateTime specyfikacji schematu XML.

Zwraca

Odpowiednik DateTimeOffset podanego ciągu.

Wyjątki

Parametr s ma wartość null.

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

Argument przekazany do tej metody nie jest zgodny z podzbiorem zaleceń W3C dla typu dateTime 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 , aby przekonwertować ciąg 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

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

Dotyczy