XmlConvert.ToDateTimeOffset Methode

Definition

Wandelt den bereitgestellten String Wert in ein DateTimeOffset Äquivalent um.

Überlädt

Name Beschreibung
ToDateTimeOffset(String, String[])

Wandelt den bereitgestellten String Wert in ein DateTimeOffset Äquivalent um.

ToDateTimeOffset(String, String)

Wandelt den bereitgestellten String Wert in ein DateTimeOffset Äquivalent um.

ToDateTimeOffset(String)

Wandelt den bereitgestellten String Wert in ein DateTimeOffset Äquivalent um.

ToDateTimeOffset(String, String[])

Wandelt den bereitgestellten String Wert in ein DateTimeOffset Äquivalent um.

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

Parameter

s
String

Die zu konvertierende Zeichenfolge

formats
String[]

Ein Array von Formaten, aus denen s konvertiert werden kann. Jedes Format in formats kann eine beliebige Teilmenge der W3C-Empfehlung für den XML-DateTime-Typ sein. (Weitere Informationen finden Sie im Abschnitt "dateTime " der XML-Schemaspezifikation.) Die Zeichenfolge s wird anhand eines dieser Formate überprüft.

Gibt zurück

Das DateTimeOffset Äquivalent der angegebenen Zeichenfolge.

Beispiele

Im folgenden Beispiel wird veranschaulicht, wie sie eine Zeichenfolge aus einer XML-Datei lesen und die ToDateTimeOffset Methode zum Konvertieren der Zeichenfolge in einen DateTimeOffset Typ verwenden. Die Eingabezeichenfolge muss vor der Konvertierung anhand eines der angegebenen Formate überprüft werden.

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

Im Beispiel wird die transactions.xml-Datei verwendet.

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

Hinweise

Wenn der in der Eingabezeichenfolge angegebene Offset zu einem Überlauf in der deserialisierten Darstellung des DateTimeOffset führt, wird eine FormatException ausgelöst.

Wenn mehr als sieben Ziffern für Bruchteile angegeben werden, wird der Wert gerundet. Beispielsweise wird 00000004 zu 00000000 und 00000005 wird 0000001.

Gilt für:

ToDateTimeOffset(String, String)

Wandelt den bereitgestellten String Wert in ein DateTimeOffset Äquivalent um.

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

Parameter

s
String

Die zu konvertierende Zeichenfolge

format
String

Das Format, aus dem s konvertiert wird. Der Formatparameter kann eine beliebige Teilmenge der W3C-Empfehlung für den XML-dateTime-Typ sein. (Weitere Informationen finden Sie im Abschnitt "dateTime " der XML-Schemaspezifikation.) Die Zeichenfolge s wird anhand dieses Formats überprüft.

Gibt zurück

Das DateTimeOffset Äquivalent der angegebenen Zeichenfolge.

Ausnahmen

s ist null.

s oder format eine leere Zeichenfolge ist oder nicht im angegebenen Format enthalten ist.

Beispiele

Im folgenden Beispiel wird veranschaulicht, wie sie eine Zeichenfolge aus einer XML-Datei lesen und die ToDateTimeOffset Methode zum Konvertieren der Zeichenfolge in einen DateTimeOffset Typ verwenden. Die Eingabezeichenfolge wird vor der Konvertierung anhand des angegebenen Formats überprüft.

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

Im Beispiel wird die transactions.xml-Datei verwendet.

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

Hinweise

Wenn der in der Eingabezeichenfolge angegebene Offset zu einem Überlauf in der deserialisierten Darstellung des DateTimeOffset führt, wird eine FormatException ausgelöst.

Wenn mehr als sieben Ziffern für Bruchteile angegeben werden, wird der Wert gerundet. Beispielsweise wird 00000004 zu 00000000 und 00000005 wird 0000001.

Gilt für:

ToDateTimeOffset(String)

Wandelt den bereitgestellten String Wert in ein DateTimeOffset Äquivalent um.

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

Parameter

s
String

Die zu konvertierende Zeichenfolge Die Zeichenfolge muss einer Teilmenge der W3C-Empfehlung für den XML-dateTime-Typ entsprechen. Weitere Informationen finden Sie im Abschnitt "dateTime " der XML-Schemaspezifikation.

Gibt zurück

Das DateTimeOffset Äquivalent der angegebenen Zeichenfolge.

Ausnahmen

s ist null.

Das an diese Methode übergebene Argument liegt außerhalb des Zulässigen Wertebereichs. Informationen zu zulässigen Werten finden Sie unter DateTimeOffset.

Das an diese Methode übergebene Argument entspricht nicht einer Teilmenge der W3C-Empfehlungen für den XML-DateTime-Typ. Weitere Informationen finden Sie im Abschnitt "dateTime " der XML-Schemaspezifikation.

Beispiele

Im folgenden Beispiel wird veranschaulicht, wie sie eine Zeichenfolge aus einer XML-Datei lesen und die ToDateTimeOffset Methode zum Konvertieren der Zeichenfolge in einen DateTimeOffset Typ verwenden.

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

Im Beispiel wird die transactions.xml-Datei verwendet.

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

Hinweise

Wenn mehr als sieben Ziffern für Bruchteile angegeben werden, wird der Wert gerundet. Beispielsweise wird 00000004 zu 00000000 und 00000005 wird 0000001.

Gilt für: