XmlConvert.ToDateTimeOffset Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Převede zadaný StringDateTimeOffset na ekvivalent.
Přetížení
| Name | Description |
|---|---|
| ToDateTimeOffset(String, String[]) |
Převede zadaný StringDateTimeOffset na ekvivalent. |
| ToDateTimeOffset(String, String) |
Převede zadaný StringDateTimeOffset na ekvivalent. |
| ToDateTimeOffset(String) |
Převede zadaný StringDateTimeOffset na ekvivalent. |
ToDateTimeOffset(String, String[])
- Zdroj:
- XmlConvert.cs
- Zdroj:
- XmlConvert.cs
- Zdroj:
- XmlConvert.cs
- Zdroj:
- XmlConvert.cs
- Zdroj:
- XmlConvert.cs
Převede zadaný StringDateTimeOffset 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 standardních nebo vlastních řetězců formátu data a času .NET, které určují povolené formáty s.
Návraty
Ekvivalent DateTimeOffset zadaného řetězce.
Výjimky
Prvek formats je neplatný nebo s neodpovídá žádnému prvku v rámci invariantních pravidel jazykové verze s výjimkou počátečního nebo koncového prázdného formats místa.
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
Toto přetížení je ekvivalentní volání DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles) s DateTimeFormatInfo.InvariantInfo a AllowLeadingWhite příznaky a AllowTrailingWhite příznaky. Počáteční a koncové prázdné znaky se ignorují, ale s musí se přesně shodovat s jedním z řetězců v formatssouboru . Tento formats parametr používá .NET standardní řetězce formátu data a času nebo vlastní řetězce formátu data a času, nikoli vzory dateTime schématu XML.
V řetězci vlastního formátu vyžadují zzzspecifikátory a zzz znaky se znaky utc se dvěma znaky. Neodpovídají návrhu ZXML UTC . Použijte specifikátor vlastního K formátu, pokud s může obsahovat Z.
Pokud posun zadaný ve vstupním řetězci způsobí přetečení v deserializované reprezentaci DateTimeOffset, je vyvolána výjimka 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)
- Zdroj:
- XmlConvert.cs
- Zdroj:
- XmlConvert.cs
- Zdroj:
- XmlConvert.cs
- Zdroj:
- XmlConvert.cs
- Zdroj:
- XmlConvert.cs
Převede zadaný StringDateTimeOffset 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
.NET standardní nebo vlastní řetězec formátu data a času, který určuje požadovaný formát s.
Návraty
Ekvivalent DateTimeOffset zadaného řetězce.
Výjimky
s je null.
format je neplatný nebo s se přesně format neshoduje s pravidly invariantní jazykové verze s výjimkou počátečních nebo koncových prázdných znaků.
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
Toto přetížení je ekvivalentní volání DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles) s DateTimeFormatInfo.InvariantInfo a AllowLeadingWhite příznaky a AllowTrailingWhite příznaky. Úvodní a koncové prázdné znaky se ignorují, ale s musí se přesně shodovat format. Tento format parametr používá .NET standardní řetězce formátu data a času nebo vlastní řetězce formátu data a času, nikoli vzory dateTime schématu XML.
V řetězci vlastního formátu vyžadují zzzspecifikátory a zzz znaky se znaky utc se dvěma znaky. Neodpovídají návrhu ZXML UTC . Použijte specifikátor vlastního K formátu, pokud s může obsahovat Z.
Pokud posun zadaný ve vstupním řetězci způsobí přetečení v deserializované reprezentaci DateTimeOffset, je vyvolána výjimka 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)
- Zdroj:
- XmlConvert.cs
- Zdroj:
- XmlConvert.cs
- Zdroj:
- XmlConvert.cs
- Zdroj:
- XmlConvert.cs
- Zdroj:
- XmlConvert.cs
Převede zadaný StringDateTimeOffset 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 naleznete v části dateTime specifikace schématu XML.
Návraty
Ekvivalent DateTimeOffset zadaného řetězce.
Výjimky
s je null.
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ě doporučení W3C pro typ dateTime XML. Další informace naleznete 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.