XmlConvert.ToDateTimeOffset Metoda

Definice

Převede zadané String DateTimeOffset na ekvivalent.

Přetížení

ToDateTimeOffset(String, String[])

Převede zadané String DateTimeOffset na ekvivalent.

ToDateTimeOffset(String, String)

Převede zadané String DateTimeOffset na ekvivalent.

ToDateTimeOffset(String)

Převede zadané String DateTimeOffset na ekvivalent.

ToDateTimeOffset(String, String[])

Převede zadané String DateTimeOffset na ekvivalent.

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

Řetězec, který chcete převést.

formats
String[]

Pole formátů, ze kterých s lze převést. Každý formát může formats být libovolnou podmnožinou doporučení W3C pro typ dateTime XML. (Další informace najdete v části dateTime specifikace schématu XML.) Řetězec s se ověří v jednom z těchto formátů.

Návraty

DateTimeOffset

Ekvivalent DateTimeOffset zadaného řetězce.

Příklady

Následující příklad ukazuje, jak číst řetězec ze souboru XML a pomocí ToDateTimeOffset metody převést řetězec na DateTimeOffset typ. Vstupní řetězec musí před převodem ověřit jeden ze zadaných formátů.

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

Příklad používá soubor 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>  

Poznámky

Pokud posun zadaný ve vstupním řetězci způsobí přetečení v deserializované reprezentaci DateTimeOffset, je vyvolána funkce FormatException.

Pokud je pro desetinné sekundy zadáno více než sedm číslic, hodnota se zaokrouhlí. Například 00000004 se stane 00000000 a 00000005 se stane 0000001.

Platí pro

ToDateTimeOffset(String, String)

Převede zadané String DateTimeOffset na ekvivalent.

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

Řetězec, který chcete převést.

format
String

Formát, ze kterého s je převeden. Parametr formátu může být libovolnou podmnožinou doporučení W3C pro typ dateTime XML. (Další informace najdete v části dateTime specifikace schématu XML.) Řetězec s se ověří v tomto formátu.

Návraty

DateTimeOffset

Ekvivalent DateTimeOffset zadaného řetězce.

Výjimky

s nebo format je prázdný řetězec nebo není v zadaném formátu.

Příklady

Následující příklad ukazuje, jak číst řetězec ze souboru XML a pomocí ToDateTimeOffset metody převést řetězec na DateTimeOffset typ. Vstupní řetězec se před převodem ověří v zadaném formátu.

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

Příklad používá soubor 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>  

Poznámky

Pokud posun zadaný ve vstupním řetězci způsobí přetečení v deserializované reprezentaci DateTimeOffset, je vyvolána funkce FormatException.

Pokud je pro desetinné sekundy zadáno více než sedm číslic, hodnota se zaokrouhlí. Například 00000004 se stane 00000000 a 00000005 se stane 0000001.

Platí pro

ToDateTimeOffset(String)

Převede zadané String DateTimeOffset na ekvivalent.

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

Řetězec, který chcete převést. Řetězec musí odpovídat podmnožině doporučení W3C pro typ dateTime XML. Další informace najdete v části dateTime specifikace schématu XML.

Návraty

DateTimeOffset

Ekvivalent DateTimeOffset zadaného řetězce.

Výjimky

Argument předaný této metodě je mimo rozsah povolených hodnot. Informace o povolených hodnotách naleznete v tématu DateTimeOffset.

Argument předaný této metodě neodpovídá podmnožině W3C Recommendations pro typ XML dateTime. Další informace najdete v části dateTime specifikace schématu XML.

Příklady

Následující příklad ukazuje, jak číst řetězec ze souboru XML a pomocí ToDateTimeOffset metody převést řetězec 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

Příklad používá soubor 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>  

Poznámky

Pokud je pro desetinné sekundy zadáno více než sedm číslic, hodnota se zaokrouhlí. Například 00000004 se stane 00000000 a 00000005 se stane 0000001.

Platí pro