XmlConvert.ToDateTimeOffset Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Converte l'oggetto fornito String in un DateTimeOffset equivalente.
Overload
| Nome | Descrizione |
|---|---|
| ToDateTimeOffset(String, String[]) |
Converte l'oggetto fornito String in un DateTimeOffset equivalente. |
| ToDateTimeOffset(String, String) |
Converte l'oggetto fornito String in un DateTimeOffset equivalente. |
| ToDateTimeOffset(String) |
Converte l'oggetto fornito String in un DateTimeOffset equivalente. |
ToDateTimeOffset(String, String[])
- Origine:
- XmlConvert.cs
- Origine:
- XmlConvert.cs
- Origine:
- XmlConvert.cs
- Origine:
- XmlConvert.cs
- Origine:
- XmlConvert.cs
Converte l'oggetto fornito String in un DateTimeOffset equivalente.
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
Parametri
- s
- String
Stringa da convertire.
- formats
- String[]
Matrice di formati da cui s è possibile convertire. Ogni formato in formats può essere qualsiasi subset della raccomandazione W3C per il tipo dateTime XML. Per altre informazioni, vedere la sezione dateTime della specifica xml Schema. La stringa s viene convalidata in base a uno di questi formati.
Valori restituiti
Equivalente DateTimeOffset della stringa fornita.
Esempio
Nell'esempio seguente viene illustrato come leggere una stringa da un file XML e utilizzare il ToDateTimeOffset metodo per convertire la stringa in un DateTimeOffset tipo. La stringa di input deve essere convalidata rispetto a uno dei formati specificati prima di essere convertita.
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
Nell'esempio viene utilizzato il file 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>
Commenti
Se l'offset specificato all'interno della stringa di input causerà un overflow nella rappresentazione deserializzata di DateTimeOffset, viene generata un'eccezione FormatException.
Quando vengono specificate più di sette cifre per i secondi frazionari, il valore viene arrotondato. Ad esempio, 00000004 diventa 0000000 e 00000005 diventa 0000001.
Si applica a
ToDateTimeOffset(String, String)
- Origine:
- XmlConvert.cs
- Origine:
- XmlConvert.cs
- Origine:
- XmlConvert.cs
- Origine:
- XmlConvert.cs
- Origine:
- XmlConvert.cs
Converte l'oggetto fornito String in un DateTimeOffset equivalente.
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
Parametri
- s
- String
Stringa da convertire.
- format
- String
Formato da cui s viene convertito. Il parametro format può essere qualsiasi subset della raccomandazione W3C per il tipo dateTime XML. Per altre informazioni, vedere la sezione dateTime della specifica xml Schema. La stringa s viene convalidata in base a questo formato.
Valori restituiti
Equivalente DateTimeOffset della stringa fornita.
Eccezioni
s è null.
s oppure format è una stringa vuota o non è nel formato specificato.
Esempio
Nell'esempio seguente viene illustrato come leggere una stringa da un file XML e utilizzare il ToDateTimeOffset metodo per convertire la stringa in un DateTimeOffset tipo. La stringa di input viene convalidata rispetto al formato specificato prima di essere convertita.
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
Nell'esempio viene utilizzato il file 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>
Commenti
Se l'offset specificato all'interno della stringa di input causerà un overflow nella rappresentazione deserializzata di DateTimeOffset, viene generata un'eccezione FormatException.
Quando vengono specificate più di sette cifre per i secondi frazionari, il valore viene arrotondato. Ad esempio, 00000004 diventa 0000000 e 00000005 diventa 0000001.
Si applica a
ToDateTimeOffset(String)
- Origine:
- XmlConvert.cs
- Origine:
- XmlConvert.cs
- Origine:
- XmlConvert.cs
- Origine:
- XmlConvert.cs
- Origine:
- XmlConvert.cs
Converte l'oggetto fornito String in un DateTimeOffset equivalente.
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
Parametri
- s
- String
Stringa da convertire. La stringa deve essere conforme a un subset della raccomandazione W3C per il tipo dateTime XML. Per altre informazioni, vedere la sezione dateTime della specifica di XML Schema.
Valori restituiti
Equivalente DateTimeOffset della stringa fornita.
Eccezioni
s è null.
L'argomento passato a questo metodo non è compreso nell'intervallo di valori consentiti. Per informazioni sui valori consentiti, vedere DateTimeOffset.
L'argomento passato a questo metodo non è conforme a un subset dei consigli W3C per il tipo dateTime XML. Per altre informazioni, vedere la sezione dateTime della specifica di XML Schema.
Esempio
Nell'esempio seguente viene illustrato come leggere una stringa da un file XML e utilizzare il ToDateTimeOffset metodo per convertire la stringa in un DateTimeOffset tipo.
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
Nell'esempio viene utilizzato il file 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>
Commenti
Quando vengono specificate più di sette cifre per i secondi frazionari, il valore viene arrotondato. Ad esempio, 00000004 diventa 0000000 e 00000005 diventa 0000001.