语言

XmlConvert.ToDateTimeOffset 方法

定义

将所提供的 String 值转换为等效项 DateTimeOffset 。

重载

名称 说明
ToDateTimeOffset(String, String[])

将所提供的 String 值转换为等效项 DateTimeOffset 。

ToDateTimeOffset(String, String)

将所提供的 String 值转换为等效项 DateTimeOffset 。

ToDateTimeOffset(String)

将所提供的 String 值转换为等效项 DateTimeOffset 。

ToDateTimeOffset(String, String[])

Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs

将所提供的 String 值转换为等效项 DateTimeOffset 。

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

参数

s
String

要转换的字符串。

formats
String[]

.NET标准或自定义日期和时间格式字符串的数组,这些字符串指定允许的格式s。

返回

DateTimeOffset提供的字符串的等效项。

例外

元素 formats 无效,或者 s 与固定区域性规则下的任何元素 formats 完全匹配,但前导或尾随空格除外。

示例

以下示例演示如何从 XML 文件读取字符串,并使用 ToDateTimeOffset 方法将字符串 DateTimeOffset 转换为类型。 在转换之前,输入字符串必须针对指定格式之一进行验证。

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

此示例使用 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>

注解

此重载等效于使用和DateTimeFormatInfo.InvariantInfoAllowLeadingWhiteAllowTrailingWhite标志进行调用。DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles) 忽略前导空格和尾随空格,但 s 必须完全匹配其中 formats一个字符串。 该formats参数使用.NET标准日期和时间格式字符串或自定义日期和时间格式字符串,而不是 XML 架构 dateTime 模式。

在自定义格式字符串中,zzz指定符和zzz说明符需要带符号数字 UTC 偏移量。 它们与 XML UTC 设计器 Z不匹配。 当可以包含Z时s,K请使用自定义格式说明符。

如果在输入字符串中指定的偏移量将导致 DateTimeOffset 的反序列化表示形式溢出,则会引发 FormatException。

如果为小数秒指定了 7 个以上的数字,则该值将舍入。 例如,00000004变为 0000000,00000005变为0000001。

适用于

ToDateTimeOffset(String, String)

Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs

将所提供的 String 值转换为等效项 DateTimeOffset 。

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

参数

s
String

要转换的字符串。

format
String

.NET指定所需格式的标准或自定义日期和时间格式s字符串。

返回

DateTimeOffset提供的字符串的等效项。

例外

s 是 null。

format 无效,或 s 与固定区域性规则下完全匹配 format ,但前导或尾随空格除外。

示例

以下示例演示如何从 XML 文件读取字符串,并使用 ToDateTimeOffset 方法将字符串 DateTimeOffset 转换为类型。 在转换之前,根据指定的格式验证输入字符串。

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

此示例使用 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>

注解

此重载等效于使用和DateTimeFormatInfo.InvariantInfoAllowLeadingWhiteAllowTrailingWhite标志进行调用。DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles) 忽略前导空格和尾随空格,但 s 必须完全匹配 format。 该format参数使用.NET标准日期和时间格式字符串或自定义日期和时间格式字符串,而不是 XML 架构 dateTime 模式。

在自定义格式字符串中,zzz指定符和zzz说明符需要带符号数字 UTC 偏移量。 它们与 XML UTC 设计器 Z不匹配。 当可以包含Z时s,K请使用自定义格式说明符。

如果在输入字符串中指定的偏移量将导致 DateTimeOffset 的反序列化表示形式溢出,则会引发 FormatException。

如果为小数秒指定了 7 个以上的数字,则该值将舍入。 例如,00000004变为 0000000,00000005变为0000001。

适用于

ToDateTimeOffset(String)

Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs

将所提供的 String 值转换为等效项 DateTimeOffset 。

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

参数

s
String

要转换的字符串。 该字符串必须符合 XML dateTime 类型的 W3C 建议的子集。 有关详细信息,请参阅 XML 架构规范的 dateTime 部分。

返回

DateTimeOffset提供的字符串的等效项。

例外

s 是 null。

传递给此方法的参数超出了允许值的范围。 有关允许值的信息,请参阅 DateTimeOffset。

传递给此方法的参数不符合 XML dateTime 类型的 W3C 建议的子集。 有关详细信息,请参阅 XML 架构规范的 dateTime 部分。

示例

以下示例演示如何从 XML 文件读取字符串,并使用 ToDateTimeOffset 方法将字符串 DateTimeOffset 转换为类型。

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

此示例使用 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>

注解

如果为小数秒指定了 7 个以上的数字,则该值将舍入。 例如,00000004变为 0000000,00000005变为0000001。

适用于