XElement.Explicit 运算符

定义

重载

Explicit(XElement to Nullable<Int64>)

将此 XElement 的值强制转换为 Nullable<T>Int64Cast the value of this XElement to a Nullable<T> of Int64.

Explicit(XElement to Nullable<Single>)

将此 XElement 的值强制转换为 Nullable<T>SingleCast the value of this XElement to a Nullable<T> of Single.

Explicit(XElement to Nullable<TimeSpan>)

将此 XElement 的值强制转换为 Nullable<T>TimeSpanCast the value of this XElement to a Nullable<T> of TimeSpan.

Explicit(XElement to Nullable<UInt32>)

将此 XElement 的值强制转换为 Nullable<T>UInt32Cast the value of this XElement to a Nullable<T> of UInt32.

Explicit(XElement to Nullable<UInt64>)

将此 XElement 的值强制转换为 Nullable<T>UInt64Cast the value of this XElement to a Nullable<T> of UInt64.

Explicit(XElement to UInt32)

将此 XElement 的值强制转换为 UInt32Cast the value of this XElement to a UInt32.

Explicit(XElement to String)

将此 XElement 的值强制转换为 StringCast the value of this XElement to a String.

Explicit(XElement to TimeSpan)

将此 XElement 的值强制转换为 TimeSpanCast the value of this XElement to a TimeSpan.

Explicit(XElement to UInt64)

将此 XElement 的值强制转换为 UInt64Cast the value of this XElement to a UInt64.

Explicit(XElement to Nullable<Int32>)

将此 XElement 的值强制转换为 Nullable<T>Int32Cast the value of this XElement to a Nullable<T> of Int32.

Explicit(XElement to Single)

将此 XElement 的值强制转换为 SingleCast the value of this XElement to a Single.

Explicit(XElement to Nullable<Guid>)

将此 XElement 的值强制转换为 Nullable<T>GuidCast the value of this XElement to a Nullable<T> of Guid.

Explicit(XElement to Nullable<Boolean>)

将此 XElement 的值强制转换为 Nullable<T>BooleanCast the value of this XElement to a Nullable<T> of Boolean.

Explicit(XElement to Nullable<Decimal>)

将此 XElement 的值强制转换为 Nullable<T>DecimalCast the value of this XElement to a Nullable<T> of Decimal.

Explicit(XElement to Boolean)

将此 XElement 的值强制转换为 BooleanCast the value of this XElement to a Boolean.

Explicit(XElement to DateTime)

将此 XElement 的值强制转换为 DateTimeCast the value of this XElement to a DateTime.

Explicit(XElement to DateTimeOffset)

将此 XAttribute 的值强制转换为 DateTimeOffsetCast the value of this XAttribute to a DateTimeOffset.

Explicit(XElement to Double)

将此 XElement 的值强制转换为 DoubleCast the value of this XElement to a Double.

Explicit(XElement to Guid)

将此 XElement 的值强制转换为 GuidCast the value of this XElement to a Guid.

Explicit(XElement to Decimal)

将此 XElement 的值强制转换为 DecimalCast the value of this XElement to a Decimal.

Explicit(XElement to Int64)

将此 XElement 的值强制转换为 Int64Cast the value of this XElement to an Int64.

Explicit(XElement to Nullable<Double>)

将此 XElement 的值强制转换为 Nullable<T>DoubleCast the value of this XElement to a Nullable<T> of Double.

Explicit(XElement to Nullable<DateTime>)

将此 XElement 的值强制转换为 Nullable<T>DateTimeCast the value of this XElement to a Nullable<T> of DateTime.

Explicit(XElement to Nullable<DateTimeOffset>)

将此 XElement 的值强制转换为 Nullable<T>DateTimeOffsetCast the value of this XElement to a Nullable<T> of DateTimeOffset.

Explicit(XElement to Int32)

将此 XElement 的值强制转换为 Int32Cast the value of this XElement to an Int32.

Explicit(XElement to Nullable<Int64>)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 Nullable<T>Int64Cast the value of this XElement to a Nullable<T> of Int64.

public:
 static explicit operator Nullable<long>(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator long? (System.Xml.Linq.XElement element);
public static explicit operator long? (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> Nullable<int64>
Public Shared Narrowing Operator CType (element As XElement) As Nullable(Of Long)

参数

element
XElement

要强制转换为 XElementNullable<T>Int64The XElement to cast to Nullable<T> of Int64.

返回

一个 Nullable<T>Int64,其中包含此 XElement 的内容。A Nullable<T> of Int64 that contains the content of this XElement.

属性

例外

元素不是 null 且不包含有效的 Int64 值。The element is not null and does not contain a valid Int64 value.

示例

下面的示例创建一个具有长整数内容的元素。The following example creates an element with long integer content. 然后,它通过强制转换为 Int64Nullable<T> 来检索该值。It then retrieves the value by casting to Nullable<T> of Int64.

XElement root = new XElement("Root",    
    new XElement("Value", 9223372036854775807)  
);  
ulong? value = (ulong?)root.Element("Value");  
Console.WriteLine("Nullable ulong: value={0}", value == null ? "null" : value.ToString());  
Dim root As XElement = _   
        <Root>  
            <Value>9223372036854775807</Value>  
        </Root>  
Dim value As Nullable(Of ULong) = CType(root.Element("Value"), Nullable(Of ULong))  
Console.WriteLine("Nullable ulong: value={0}", IIf(value.HasValue, value.ToString(), "null"))  

该示例产生下面的输出:This example produces the following output:

Nullable ulong: value=9223372036854775807  

另请参阅

Explicit(XElement to Nullable<Single>)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 Nullable<T>SingleCast the value of this XElement to a Nullable<T> of Single.

public:
 static explicit operator Nullable<float>(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator float? (System.Xml.Linq.XElement element);
public static explicit operator float? (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> Nullable<single>
Public Shared Narrowing Operator CType (element As XElement) As Nullable(Of Single)

参数

element
XElement

要强制转换为 XElementNullable<T>SingleThe XElement to cast to Nullable<T> of Single.

返回

一个 Nullable<T>Single,其中包含此 XElement 的内容。A Nullable<T> of Single that contains the content of this XElement.

属性

例外

元素不是 null 且不包含有效的 Single 值。The element is not null and does not contain a valid Single value.

示例

下面的示例创建一个具有单精度浮点数内容的元素。The following example creates an element with single precision floating point content. 然后,它通过强制转换为 SingleNullable<T> 来检索该值。It then retrieves the value by casting to Nullable<T> of Single.

XElement root = new XElement("Root",   
    new XElement("Value", 3.402823e38)  
);  
float? value = (float?)root.Element("Value");  
Console.WriteLine("Nullable Single: value={0}", value == null ? "null" : value.ToString());  
Dim root As XElement = _   
        <Root>  
            <Value>3.402823e38</Value>  
        </Root>  
Dim value As Nullable(Of Single) = CType(root.Element("Value"), Nullable(Of Single))  
Console.WriteLine("Nullable Single: value={0}", IIf(value.HasValue, value.ToString(), "null"))  

该示例产生下面的输出:This example produces the following output:

Nullable Single: value=3.402823E+38  

另请参阅

Explicit(XElement to Nullable<TimeSpan>)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 Nullable<T>TimeSpanCast the value of this XElement to a Nullable<T> of TimeSpan.

public:
 static explicit operator Nullable<TimeSpan>(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator TimeSpan? (System.Xml.Linq.XElement element);
public static explicit operator TimeSpan? (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> Nullable<TimeSpan>
Public Shared Narrowing Operator CType (element As XElement) As Nullable(Of TimeSpan)

参数

element
XElement

要强制转换为 XElementNullable<T>TimeSpanThe XElement to cast to Nullable<T> of TimeSpan.

返回

一个 Nullable<T>TimeSpan,其中包含此 XElement 的内容。A Nullable<T> of TimeSpan that contains the content of this XElement.

属性

例外

元素不是 null 且不包含有效的 TimeSpan 值。The element is not null and does not contain a valid TimeSpan value.

示例

下面的示例创建一个具有时间跨度内容的元素。The following example creates an element with time span content. 然后,它通过强制转换为 TimeSpanNullable<T> 来检索该值。It then retrieves the value by casting to Nullable<T> of TimeSpan.

XElement root = new XElement("Root",  
    new XElement("Value", new TimeSpan(1, 5, 30))  
);  
TimeSpan? value = (TimeSpan?)root.Element("Value");  
Console.WriteLine("Nullable TimeSpan: value={0}", value == null ? "null" : value.ToString());  
Dim root As XElement = _  
    <Root>  
        <Value><%= New TimeSpan(1, 5, 30) %></Value>  
    </Root>  
Dim value As Nullable(Of TimeSpan) = CType(root.Element("Value"), Nullable(Of TimeSpan))  
Console.WriteLine("Nullable TimeSpan: value={0}", IIf(value.HasValue, value.ToString(), "null"))  

该示例产生下面的输出:This example produces the following output:

Nullable TimeSpan: value=01:05:30  

注解

包含时间跨度内容的属性或元素的值空间与持续时间内容密切相关,如 ISO 8601 中所述。The value space of an attribute or element that contains time span content is closely related to duration content as described in ISO 8601. 在创建包含时间跨度内容的属性或元素时,将根据 W3C 规范设置属性或元素值的格式。When creating an attribute or element that contains time span content, the attribute or element values are formatted per the W3C specification. 有关更多详细信息,请参阅 W3C 规范。Please see the W3C specification for more details.

从特性或元素强制转换为 TimeSpanNullable<T> 时,行为是不严格的。Behavior is lax when casting to a Nullable<T> of TimeSpan from an attribute or element. 即使属性或元素值的格式设置与 W3C 规范的格式完全不同,该值也会适当地转换为 TimeSpanNullable<T>Even if the attribute or element value is not formatted exactly per the W3C specification, the value is appropriately converted to a Nullable<T> of TimeSpan.

另请参阅

Explicit(XElement to Nullable<UInt32>)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 Nullable<T>UInt32Cast the value of this XElement to a Nullable<T> of UInt32.

public:
 static explicit operator Nullable<System::UInt32>(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator uint? (System.Xml.Linq.XElement element);
public static explicit operator uint? (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> Nullable<uint32>
Public Shared Narrowing Operator CType (element As XElement) As Nullable(Of UInteger)

参数

element
XElement

要强制转换为 XElementNullable<T>UInt32The XElement to cast to Nullable<T> of UInt32.

返回

一个 Nullable<T>UInt32,其中包含此 XElement 的内容。A Nullable<T> of UInt32 that contains the content of this XElement.

属性

例外

元素不是 null 且不包含有效的 UInt32 值。The element is not null and does not contain a valid UInt32 value.

示例

下面的示例创建一个具有无符号整数内容的元素。The following example creates an element with unsigned integer content. 然后,它通过强制转换为 UInt32Nullable<T> 来检索该值。It then retrieves the value by casting to Nullable<T> of UInt32.

XElement root = new XElement("Root",   
    new XElement("Value", 4294967295)  
);  
uint? value = (uint?)root.Element("Value");  
Console.WriteLine("Nullable uint: value={0}", value == null ? "null" : value.ToString());  
Dim root As XElement = _   
        <Root>  
            <Value>4294967295</Value>  
        </Root>  
Dim value As Nullable(Of UInteger) = CType(root.Element("Value"), Nullable(Of UInteger))  
Console.WriteLine("Nullable uint: value={0}", IIf(value.HasValue, value.ToString(), "null"))  

该示例产生下面的输出:This example produces the following output:

Nullable uint: value=4294967295  

另请参阅

Explicit(XElement to Nullable<UInt64>)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 Nullable<T>UInt64Cast the value of this XElement to a Nullable<T> of UInt64.

public:
 static explicit operator Nullable<System::UInt64>(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator ulong? (System.Xml.Linq.XElement element);
public static explicit operator ulong? (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> Nullable<uint64>
Public Shared Narrowing Operator CType (element As XElement) As Nullable(Of ULong)

参数

element
XElement

要强制转换为 XElementNullable<T>UInt64The XElement to cast to Nullable<T> of UInt64.

返回

一个 Nullable<T>UInt64,其中包含此 XElement 的内容。A Nullable<T> of UInt64 that contains the content of this XElement.

属性

例外

元素不是 null 且不包含有效的 UInt64 值。The element is not null and does not contain a valid UInt64 value.

示例

下面的示例创建一个元素,该元素具有无符号长整数内容。The following example creates an element with unsigned long integer content. 然后,它通过强制转换为 UInt64Nullable<T> 来检索该值。It then retrieves the value by casting to Nullable<T> of UInt64.

XElement root = new XElement("Root",   
    new XElement("Value", 9223372036854775807)  
);  
ulong? value = (ulong?)root.Element("Value");  
Console.WriteLine("Nullable ulong: value={0}", value == null ? "null" : value.ToString());  
Dim root As XElement = _   
        <Root>  
            <Value>9223372036854775807</Value>  
        </Root>  
  
Dim value As Nullable(Of ULong) = CType(root.Element("Value"), Nullable(Of ULong))  
Console.WriteLine("Nullable ulong: value={0}", IIf(value.HasValue, value.ToString(), "null"))  

该示例产生下面的输出:This example produces the following output:

Nullable ulong: value=9223372036854775807  

另请参阅

Explicit(XElement to UInt32)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 UInt32Cast the value of this XElement to a UInt32.

public:
 static explicit operator System::UInt32(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator uint (System.Xml.Linq.XElement element);
public static explicit operator uint (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> uint32
Public Shared Narrowing Operator CType (element As XElement) As UInteger

参数

element
XElement

要强制转换为 XElementUInt32The XElement to cast to UInt32.

返回

一个 UInt32,其中包含此 XElement 的内容。A UInt32 that contains the content of this XElement.

属性

例外

元素不包含有效的 UInt32 值。The element does not contain a valid UInt32 value.

element 参数为 nullThe element parameter is null.

示例

下面的示例创建一个具有无符号整数内容的元素。The following example creates an element with unsigned integer content. 然后,它通过强制转换为 UInt32来检索值。It then retrieves the value by casting to UInt32.

XElement root = new XElement("Root", 4294967295);  
uint value = (uint)root;  
Console.WriteLine("value={0}", value);  
Dim root As XElement = <Root>4294967295</Root>  
Dim value As UInteger = CUInt(root)  
Console.WriteLine("value={0}", value)  

该示例产生下面的输出:This example produces the following output:

value=4294967295  

另请参阅

Explicit(XElement to String)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 StringCast the value of this XElement to a String.

public:
 static explicit operator System::String ^(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator string (System.Xml.Linq.XElement element);
public static explicit operator string (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> string
Public Shared Narrowing Operator CType (element As XElement) As String

参数

element
XElement

要强制转换为 XElementStringThe XElement to cast to String.

返回

一个 String,其中包含此 XElement 的内容。A String that contains the content of this XElement.

属性

示例

下面的示例创建一个包含字符串内容的元素。The following example creates an element with string content. 然后,它通过强制转换为 String来检索值。It then retrieves the value by casting to String.

XElement root = XElement.Parse("<Root>abc <b>def </b>ghi</Root>");  
Console.WriteLine("(string)root={0}", (string)root);  
Dim root As XElement = <Root>abc <b>def </b>ghi</Root>  
Console.WriteLine("(string)root={0}", root.Value)  

该示例产生下面的输出:This example produces the following output:

(string)root=abc def ghi  

注解

如果 XElement 有子级,则返回该元素的所有文本和子代文本的连接字符串值。If the XElement has children, the concatenated string value of all of the element's text and descendant's text is returned.

另请参阅

Explicit(XElement to TimeSpan)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 TimeSpanCast the value of this XElement to a TimeSpan.

public:
 static explicit operator TimeSpan(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator TimeSpan (System.Xml.Linq.XElement element);
public static explicit operator TimeSpan (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> TimeSpan
Public Shared Narrowing Operator CType (element As XElement) As TimeSpan

参数

element
XElement

要强制转换为 XElementTimeSpanThe XElement to cast to TimeSpan.

返回

一个 TimeSpan,其中包含此 XElement 的内容。A TimeSpan that contains the content of this XElement.

属性

例外

元素不包含有效的 TimeSpan 值。The element does not contain a valid TimeSpan value.

element 参数为 nullThe element parameter is null.

示例

下面的示例创建一个具有时间跨度内容的元素。The following example creates an element with time span content. 然后,它通过强制转换为 TimeSpan来检索值。It then retrieves the value by casting to TimeSpan.

XElement root = new XElement("Root", new TimeSpan(1, 5, 30));  
TimeSpan value = (TimeSpan)root;  
Console.WriteLine("value={0}", value);  
Dim root As XElement = <Root><%= New TimeSpan(1, 5, 30) %></Root>  
Dim value As TimeSpan = CType(root, TimeSpan)  
Console.WriteLine("value={0}", value)  

该示例产生下面的输出:This example produces the following output:

value=01:05:30  

注解

包含时间跨度内容的属性或元素的值空间与持续时间内容密切相关,如 ISO 8601 中所述。The value space of an attribute or element that contains time span content is closely related to duration content as described in ISO 8601. 在创建包含时间跨度内容的属性或元素时,将根据 W3C 规范设置属性或元素值的格式。When creating an attribute or element that contains time span content, the attribute or element values are formatted per the W3C specification. 有关更多详细信息,请参阅 W3C 规范。Please see the W3C specification for more details.

从特性或元素强制转换到 TimeSpan 时,行为是不严格的。Behavior is lax when casting to a TimeSpan from an attribute or element. 即使属性或元素值的格式设置与 W3C 规范的格式完全不同,该值也会适当地转换为 TimeSpanEven if the attribute or element value is not formatted exactly per the W3C specification, the value is appropriately converted to a TimeSpan.

另请参阅

Explicit(XElement to UInt64)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 UInt64Cast the value of this XElement to a UInt64.

public:
 static explicit operator System::UInt64(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator ulong (System.Xml.Linq.XElement element);
public static explicit operator ulong (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> uint64
Public Shared Narrowing Operator CType (element As XElement) As ULong

参数

element
XElement

要强制转换为 XElementUInt64The XElement to cast to UInt64.

返回

一个 UInt64,其中包含此 XElement 的内容。A UInt64 that contains the content of this XElement.

属性

例外

元素不包含有效的 UInt64 值。The element does not contain a valid UInt64 value.

element 参数为 nullThe element parameter is null.

示例

下面的示例创建一个元素,该元素具有无符号长整数内容。The following example creates an element with unsigned long integer content. 然后,它通过强制转换为 UInt64来检索值。It then retrieves the value by casting to UInt64.

XElement root = new XElement("Root", 18446744073709551615);  
ulong value = (ulong)root;  
Console.WriteLine("value={0}", value);  
Dim root As XElement = <Root>18446744073709551615</Root>  
Dim value As ULong = CULng(root)  
Console.WriteLine("value={0}", value)  

该示例产生下面的输出:This example produces the following output:

value=18446744073709551615  

另请参阅

Explicit(XElement to Nullable<Int32>)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 Nullable<T>Int32Cast the value of this XElement to a Nullable<T> of Int32.

public:
 static explicit operator Nullable<int>(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator int? (System.Xml.Linq.XElement element);
public static explicit operator int? (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> Nullable<int>
Public Shared Narrowing Operator CType (element As XElement) As Nullable(Of Integer)

参数

element
XElement

要强制转换为 XElementNullable<T>Int32The XElement to cast to Nullable<T> of Int32.

返回

一个 Nullable<T>Int32,其中包含此 XElement 的内容。A Nullable<T> of Int32 that contains the content of this XElement.

属性

例外

元素不是 null 且不包含有效的 Int32 值。The element is not null and does not contain a valid Int32 value.

示例

下面的示例创建一个具有无符号整数内容的元素。The following example creates an element with unsigned integer content. 然后,它通过强制转换为 Int32Nullable<T> 来检索该值。It then retrieves the value by casting to Nullable<T> of Int32.

XElement root = new XElement("Root",   
    new XElement("Value", 2147483647)  
);  
int? value = (int?)root.Element("Value");  
Console.WriteLine("Nullable integer: value={0}", value == null ? "null" : value.ToString());  
Dim root As XElement = _   
        <Root>  
            <Value>2147483647</Value>  
        </Root>  
Dim value As Nullable(Of Integer) = CType(root.Element("Value"), Nullable(Of Integer))  
Console.WriteLine("Nullable integer: value={0}", IIf(value.HasValue, value.ToString(), "null"))  
  

该示例产生下面的输出:This example produces the following output:

Nullable integer: value=2147483647  

另请参阅

Explicit(XElement to Single)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 SingleCast the value of this XElement to a Single.

public:
 static explicit operator float(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator float (System.Xml.Linq.XElement element);
public static explicit operator float (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> single
Public Shared Narrowing Operator CType (element As XElement) As Single

参数

element
XElement

要强制转换为 XElementSingleThe XElement to cast to Single.

返回

一个 Single,其中包含此 XElement 的内容。A Single that contains the content of this XElement.

属性

例外

元素不包含有效的 Single 值。The element does not contain a valid Single value.

element 参数为 nullThe element parameter is null.

示例

下面的示例创建一个具有单精度浮点数内容的元素。The following example creates an element with single precision floating point content. 然后,它通过强制转换为 Single来检索值。It then retrieves the value by casting to Single.

XElement root = new XElement("Root", 3.402823e38);  
float value = (float)root;  
Console.WriteLine("value={0}", value);  
Dim root As XElement = <Root>3.402823E+38</Root>  
Dim value As Single = CSng(root)  
Console.WriteLine("value={0}", value)  

该示例产生下面的输出:This example produces the following output:

value=3.402823E+38  

另请参阅

Explicit(XElement to Nullable<Guid>)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 Nullable<T>GuidCast the value of this XElement to a Nullable<T> of Guid.

public:
 static explicit operator Nullable<Guid>(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator Guid? (System.Xml.Linq.XElement element);
public static explicit operator Guid? (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> Nullable<Guid>
Public Shared Narrowing Operator CType (element As XElement) As Nullable(Of Guid)

参数

element
XElement

要强制转换为 XElementNullable<T>GuidThe XElement to cast to Nullable<T> of Guid.

返回

一个 Nullable<T>Guid,其中包含此 XElement 的内容。A Nullable<T> of Guid that contains the content of this XElement.

属性

例外

元素不是 null 且不包含有效的 Guid 值。The element is not null and does not contain a valid Guid value.

示例

下面的示例创建一个具有 guid 内容的元素。The following example creates an element with guid content. 然后,它通过强制转换为 GuidNullable<T> 来检索该值。It then retrieves the value by casting to Nullable<T> of Guid.

XElement root = new XElement("Root",  
    new XElement("Value", new Guid("3c1cc55b-baff-4b7a-9d17-077af3aa5730"))  
);  
Guid? value = (Guid?)root.Element("Value");  
Console.WriteLine("Nullable Guid: value={0}", value == null ? "null" : value.ToString());  
Dim root As XElement = _   
    <Root>  
        <Value><%= New Guid("3c1cc55b-baff-4b7a-9d17-077af3aa5730") %></Value>  
    </Root>  
Dim value As Nullable(Of Guid) = CType(root.Element("Value"), Nullable(Of Guid))  
Console.WriteLine("Nullable Guid: value={0}", IIf(value.HasValue, value.ToString(), "null"))  

该示例产生下面的输出:This example produces the following output:

Nullable Guid: value=3c1cc55b-baff-4b7a-9d17-077af3aa5730  

另请参阅

Explicit(XElement to Nullable<Boolean>)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 Nullable<T>BooleanCast the value of this XElement to a Nullable<T> of Boolean.

public:
 static explicit operator Nullable<bool>(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator bool? (System.Xml.Linq.XElement element);
public static explicit operator bool? (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> Nullable<bool>
Public Shared Narrowing Operator CType (element As XElement) As Nullable(Of Boolean)

参数

element
XElement

要强制转换为 XElementNullable<T>BooleanThe XElement to cast to Nullable<T> of Boolean.

返回

一个 Nullable<T>Boolean,其中包含此 XElement 的内容。A Nullable<T> of Boolean that contains the content of this XElement.

属性

例外

元素不是 null 且不包含有效的 Boolean 值。The element is not null and does not contain a valid Boolean value.

示例

下面的示例创建一个具有布尔内容的元素。The following example creates an element with boolean content. 然后,它通过强制转换为 BooleanNullable<T> 来检索该值。It then retrieves the value by casting to Nullable<T> of Boolean.

XElement root = new XElement("Root",   
    new XElement("BoolValue1", true),  
    new XElement("BoolValue2", false));  
bool? bool1 = (bool?)root.Element("BoolValue1");  
bool? bool2 = (bool?)root.Element("BoolValue2");  
Console.WriteLine("Nullable Boolean: value1={0}", bool1);  
Console.WriteLine("Nullable Boolean: value2={0}", bool2);  
Dim root As XElement = _   
        <Root>  
            <BoolValue1>true</BoolValue1>  
            <BoolValue2>false</BoolValue2>  
        </Root>  
  
Dim value1 As Nullable(Of Boolean) = CType(root.Element("BoolValue1"), Nullable(Of Boolean))  
Dim value2 As Nullable(Of Boolean) = CType(root.Element("BoolValue2"), Nullable(Of Boolean))  
Console.WriteLine("Nullable Boolean: value1={0}", IIf(value1.HasValue, value1.ToString(), "null"))  
Console.WriteLine("Nullable Boolean: value2={0}", IIf(value2.HasValue, value2.ToString(), "null"))  

该示例产生下面的输出:This example produces the following output:

Nullable Boolean: value1=True  
Nullable Boolean: value2=False  

注解

在从属性或元素转换为 Boolean Nullable<T> 时,允许的值为 "0"、"1" 和在修整和转换为小写后生成 "true" 或 "false" 的任何字符串。When converting to Nullable<T> of Boolean from an attribute or element, allowed values are "0", "1", and any string that produces "true" or "false" after trimming and conversion to lower case.

另请参阅

Explicit(XElement to Nullable<Decimal>)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 Nullable<T>DecimalCast the value of this XElement to a Nullable<T> of Decimal.

public:
 static explicit operator Nullable<System::Decimal>(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator decimal? (System.Xml.Linq.XElement element);
public static explicit operator decimal? (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> Nullable<decimal>
Public Shared Narrowing Operator CType (element As XElement) As Nullable(Of Decimal)

参数

element
XElement

要强制转换为 XElementNullable<T>DecimalThe XElement to cast to Nullable<T> of Decimal.

返回

一个 Nullable<T>Decimal,其中包含此 XElement 的内容。A Nullable<T> of Decimal that contains the content of this XElement.

属性

例外

元素不是 null 且不包含有效的 Decimal 值。The element is not null and does not contain a valid Decimal value.

示例

下面的示例创建一个包含 decimal 内容的元素。The following example creates an element with decimal content. 然后,它通过强制转换为 DecimalNullable<T> 来检索该值。It then retrieves the value by casting to Nullable<T> of Decimal.

XElement root = new XElement("Root",  
    new XElement("Value", "79228162514264337593543950335")  
);  
decimal? value = (decimal?)root.Element("Value");  
Console.WriteLine("Nullable decimal: value={0}", value == null ? "null" : value.ToString());  
Dim root As XElement = _   
    <Root>  
        <Value>79228162514264337593543950335</Value>  
    </Root>  
Dim value As Nullable(Of Decimal) = CType(root.Element("Value"), Nullable(Of Decimal))  
Console.WriteLine("Nullable decimal: value={0}", IIf(value.HasValue, value.ToString(), "null"))  

该示例产生下面的输出:This example produces the following output:

Nullable decimal: value=79228162514264337593543950335  

另请参阅

Explicit(XElement to Boolean)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 BooleanCast the value of this XElement to a Boolean.

public:
 static explicit operator bool(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator bool (System.Xml.Linq.XElement element);
public static explicit operator bool (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> bool
Public Shared Narrowing Operator CType (element As XElement) As Boolean

参数

element
XElement

要强制转换为 XElementBooleanThe XElement to cast to Boolean.

返回

一个 Boolean,其中包含此 XElement 的内容。A Boolean that contains the content of this XElement.

属性

例外

元素不包含有效的 Boolean 值。The element does not contain a valid Boolean value.

element 参数为 nullThe element parameter is null.

示例

下面的示例创建一些具有布尔值的元素。The following example creates some elements with boolean values. 然后,将它们强制转换为 BooleanIt then casts them to Boolean.

XElement root = new XElement("Root",   
    new XElement("BoolValue1", true),  
    new XElement("BoolValue2", false)  
);  
bool bool1 = (bool)root.Element("BoolValue1");  
bool bool2 = (bool)root.Element("BoolValue2");  
Console.WriteLine("(bool)BoolValue1={0}", bool1);  
Console.WriteLine("(bool)BoolValue2={0}", bool2);  
Dim root As XElement = _   
        <Root>  
            <BoolValue1>true</BoolValue1>  
            <BoolValue2>false</BoolValue2>  
        </Root>  
Dim bool1 As Boolean = CBool(root.Element("BoolValue1"))  
Dim bool2 As Boolean = CBool(root.Element("BoolValue2"))  
Console.WriteLine("(bool)BoolValue1={0}", bool1)  
Console.WriteLine("(bool)BoolValue2={0}", bool2)  

该示例产生下面的输出:This example produces the following output:

(bool)BoolValue1=True  
(bool)BoolValue2=False  

注解

从特性或元素转换到 Boolean 时,允许的值为 "0"、"1" 和在修整和转换为小写后生成 "true" 或 "false" 的任何字符串。When converting to Boolean from an attribute or element, allowed values are "0", "1", and any string that produces "true" or "false" after trimming and conversion to lower case.

另请参阅

Explicit(XElement to DateTime)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 DateTimeCast the value of this XElement to a DateTime.

public:
 static explicit operator DateTime(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator DateTime (System.Xml.Linq.XElement element);
public static explicit operator DateTime (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> DateTime
Public Shared Narrowing Operator CType (element As XElement) As DateTime

参数

element
XElement

要强制转换为 XElementDateTimeThe XElement to cast to DateTime.

返回

一个 DateTime,其中包含此 XElement 的内容。A DateTime that contains the content of this XElement.

属性

例外

元素不包含有效的 DateTime 值。The element does not contain a valid DateTime value.

element 参数为 nullThe element parameter is null.

示例

下面的示例创建一个具有日期和时间内容的元素。The following example creates an element with date and time content. 然后,它将其强制转换为 DateTime 来检索值。It then casts it to DateTime to retrieve the value.

// Behavior is strict when formatting an XML element or attribute from a DateTime,  
// but behavior is lax when casting to a DateTime from an element or attribute.  
XElement root = new XElement("Root", new DateTime(2006, 10, 6, 12, 30, 0));  
Console.WriteLine(root);  
  
// Cast from a strictly formatted XML element.  
DateTime dt = (DateTime)root;  
Console.WriteLine("dt={0}", dt);  
Console.WriteLine("-----");  
  
// If root is formatted in some different way:  
XElement dtElement = new XElement("OrderDate", "October 6, 2006");  
Console.WriteLine(dtElement);  
DateTime orderDate = (DateTime)dtElement;  
Console.WriteLine("orderDate={0:d}", orderDate);  
' Behavior is strict when formatting an XML element or attribute from a DateTime,  
' but behavior is lax when casting to a DateTime from an element or attribute.  
Dim root As XElement = <Root><%= New DateTime(2006, 10, 6, 12, 30, 0) %></Root>  
Console.WriteLine(root)  
  
' Cast from a strictly formatted XML element.  
Dim dt As DateTime = CType(root, DateTime)  
Console.WriteLine("dt={0}", dt)  
Console.WriteLine("-----")  
  
' If root is formatted in some different way:  
Dim dtElement As XElement = <OrderDate>October 6, 2006</OrderDate>  
Console.WriteLine(dtElement)  
Dim orderDate As DateTime = CType(dtElement, DateTime)  
Console.WriteLine("orderDate={0:d}", orderDate)  

该示例产生下面的输出:This example produces the following output:

<Root>2006-10-06T12:30:00</Root>  
dt=10/6/2006 12:30:00 PM  
-----  
<OrderDate>October 6, 2006</OrderDate>  
orderDate=10/6/2006  

注解

包含日期和时间内容的属性或元素的值空间与 ISO 8601 中所述的日期和时间密切相关。The value space of an attribute or element that contains date and time content is closely related to the dates and times described in ISO 8601. 当创建包含日期和时间内容的属性或元素时,将根据 W3C 规范设置属性或元素值的格式。When creating an attribute or element that contains date and time content, the attribute or element values are formatted per the W3C specification. 有关更多详细信息,请参阅 W3C 规范。See the W3C specification for more details.

从特性或元素强制转换到 DateTime 时,行为是不严格的。Behavior is lax when casting to a DateTime from an attribute or element. 即使属性或元素值的格式设置与 W3C 规范的格式完全不同,该值也会适当地转换为 DateTimeEven if the attribute or element value is not formatted exactly per the W3C specification, the value is appropriately converted to a DateTime.

此转换运算符使用 CultureInfo.InvariantCultureDateTime进行转换。This conversion operator uses CultureInfo.InvariantCulture to convert from a DateTime.

另请参阅

Explicit(XElement to DateTimeOffset)

重要

此 API 不符合 CLS。

将此 XAttribute 的值强制转换为 DateTimeOffsetCast the value of this XAttribute to a DateTimeOffset.

public:
 static explicit operator DateTimeOffset(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator DateTimeOffset (System.Xml.Linq.XElement element);
public static explicit operator DateTimeOffset (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> DateTimeOffset
Public Shared Narrowing Operator CType (element As XElement) As DateTimeOffset

参数

element
XElement

要强制转换为 XElementDateTimeOffsetThe XElement to cast to DateTimeOffset.

返回

一个 DateTimeOffset,其中包含此 XElement 的内容。A DateTimeOffset that contains the content of this XElement.

属性

例外

元素不包含有效的 DateTimeOffset 值。The element does not contain a valid DateTimeOffset value.

element 参数为 nullThe element parameter is null.

示例

下面的示例创建一个具有日期和时间内容的元素。The following example creates an element with date and time content. 然后,它强制转换为 DateTimeOffset 以检索值。It then casts to DateTimeOffset to retrieve the value.

XElement root = new XElement("Root",  
    new XElement("Child", new DateTimeOffset(new DateTime(2006, 10, 6, 12, 30, 0)))  
);  
Console.WriteLine(root);  
  
DateTimeOffset dt = (DateTimeOffset)root.Element("Child");  
Console.WriteLine("dt={0}", dt);  
Dim root As XElement = _  
    <Root>  
        <Child><%= New DateTimeOffset(New DateTime(2006, 10, 6, 12, 30, 0)) %></Child>  
    </Root>  
Console.WriteLine(root)  
  
Dim dt As DateTimeOffset = CType(root.<Child>(0), DateTimeOffset)  
Console.WriteLine("dt={0}", dt)  

该示例产生下面的输出:This example produces the following output:

<Root>  
  <Child>2006-10-06T12:30:00-07:00</Child>  
</Root>  
dt=10/6/2006 12:30:00 PM -07:00  

注解

此转换运算符使用 XmlConvert 类来执行转换。This conversion operator uses the XmlConvert class to do the conversion.

另请参阅

Explicit(XElement to Double)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 DoubleCast the value of this XElement to a Double.

public:
 static explicit operator double(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator double (System.Xml.Linq.XElement element);
public static explicit operator double (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> double
Public Shared Narrowing Operator CType (element As XElement) As Double

参数

element
XElement

要强制转换为 XElementDoubleThe XElement to cast to Double.

返回

一个 Double,其中包含此 XElement 的内容。A Double that contains the content of this XElement.

属性

例外

元素不包含有效的 Double 值。The element does not contain a valid Double value.

element 参数为 nullThe element parameter is null.

示例

下面的示例创建 double 内容的属性。The following example creates an attribute with double content. 然后,它通过强制转换为 Double来检索值。It then retrieves the value by casting to Double.

XElement root = new XElement("Root", 1.79769313486231e308);  
double value = (double)root;  
Console.WriteLine("value={0}", value);  
Dim root As XElement = <Root>1.79769313486231E+308</Root>  
Dim value As Double = CDbl(root)  
Console.WriteLine("value={0}", value)  

该示例产生下面的输出:This example produces the following output:

value=1.79769313486231E+308  

另请参阅

Explicit(XElement to Guid)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 GuidCast the value of this XElement to a Guid.

public:
 static explicit operator Guid(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator Guid (System.Xml.Linq.XElement element);
public static explicit operator Guid (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> Guid
Public Shared Narrowing Operator CType (element As XElement) As Guid

参数

element
XElement

要强制转换为 XElementGuidThe XElement to cast to Guid.

返回

一个 Guid,其中包含此 XElement 的内容。A Guid that contains the content of this XElement.

属性

例外

元素不包含有效的 Guid 值。The element does not contain a valid Guid value.

element 参数为 nullThe element parameter is null.

示例

下面的示例创建一个元素,并将 guid 作为内容。The following example creates an element with a guid as content. 然后,它通过强制转换为 Guid来检索值。It then retrieves the value by casting to Guid.

XElement root = new XElement("Root", new Guid("3c1cc55b-baff-4b7a-9d17-077af3aa5730"));  
Guid value = (Guid)root;  
Console.WriteLine("value={0}", value);  
Dim root As XElement = <Root><%= New Guid("3c1cc55b-baff-4b7a-9d17-077af3aa5730") %></Root>  
Dim value As Guid = CType(root, Guid)  
Console.WriteLine("value={0}", value)  

该示例产生下面的输出:This example produces the following output:

value=3c1cc55b-baff-4b7a-9d17-077af3aa5730  

另请参阅

Explicit(XElement to Decimal)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 DecimalCast the value of this XElement to a Decimal.

public:
 static explicit operator System::Decimal(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator decimal (System.Xml.Linq.XElement element);
public static explicit operator decimal (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> decimal
Public Shared Narrowing Operator CType (element As XElement) As Decimal

参数

element
XElement

要强制转换为 XElementDecimalThe XElement to cast to Decimal.

返回

一个 Decimal,其中包含此 XElement 的内容。A Decimal that contains the content of this XElement.

属性

例外

元素不包含有效的 Decimal 值。The element does not contain a valid Decimal value.

element 参数为 nullThe element parameter is null.

示例

下面的示例创建一个具有十进制值的元素。The following example creates an element with a decimal value. 然后,它通过强制转换为 Decimal来检索属性的值。It then retrieves the value of the attribute by casting to Decimal.

XElement root = new XElement("Root", "79228162514264337593543950335");  
decimal value = (decimal)root;  
Console.WriteLine("value={0}", value);  
Dim root As XElement = <Root>79228162514264337593543950335</Root>  
Dim value As Decimal = CDec(root)  
Console.WriteLine("value={0}", value)  
  

该示例产生下面的输出:This example produces the following output:

value=79228162514264337593543950335  

另请参阅

Explicit(XElement to Int64)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 Int64Cast the value of this XElement to an Int64.

public:
 static explicit operator long(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator long (System.Xml.Linq.XElement element);
public static explicit operator long (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> int64
Public Shared Narrowing Operator CType (element As XElement) As Long

参数

element
XElement

要强制转换为 XElementInt64The XElement to cast to Int64.

返回

一个 Int64,其中包含此 XElement 的内容。A Int64 that contains the content of this XElement.

属性

例外

元素不包含有效的 Int64 值。The element does not contain a valid Int64 value.

element 参数为 nullThe element parameter is null.

示例

下面的示例创建一个具有长整数作为内容的元素。The following example creates an element with a long integer as content. 然后,它通过将转换为 Int64来检索元素的值。It then retrieves the value of the element by casting to Int64.

XElement root = new XElement("Root", 9223372036854775807);  
long value = (long)root;  
Console.WriteLine("value={0}", value);  
Dim root As XElement = <Root>9223372036854775807</Root>  
Dim value As Long = CLng(root)  
Console.WriteLine("value={0}", value)  

该示例产生下面的输出:This example produces the following output:

value=9223372036854775807  

另请参阅

Explicit(XElement to Nullable<Double>)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 Nullable<T>DoubleCast the value of this XElement to a Nullable<T> of Double.

public:
 static explicit operator Nullable<double>(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator double? (System.Xml.Linq.XElement element);
public static explicit operator double? (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> Nullable<double>
Public Shared Narrowing Operator CType (element As XElement) As Nullable(Of Double)

参数

element
XElement

要强制转换为 XElementNullable<T>DoubleThe XElement to cast to Nullable<T> of Double.

返回

一个 Nullable<T>Double,其中包含此 XElement 的内容。A Nullable<T> of Double that contains the content of this XElement.

属性

例外

元素不是 null 且不包含有效的 Double 值。The element is not null and does not contain a valid Double value.

示例

下面的示例创建一个具有双精度浮点数内容的元素。The following example creates an element with double precision floating point content. 然后,它通过强制转换为 DoubleNullable<T> 来检索该值。It then retrieves the value by casting to Nullable<T> of Double.

XElement root = new XElement("Root",  
    new XElement("Value", 1.79769313486231e308)  
);  
double? value = (double?)root.Element("Value");  
Console.WriteLine("Nullable double: value={0}", value == null ? "null" : value.ToString());  
Dim root As XElement = _  
        <Root>  
            <Value>1.79769313486231e308</Value>  
        </Root>  
  
Dim value As Nullable(Of Double) = CType(root.Element("Value"), Nullable(Of Double))  
Console.WriteLine("Nullable double: value={0}", IIf(value.HasValue, value.ToString(), "null"))  

该示例产生下面的输出:This example produces the following output:

Nullable double: value=1.79769313486231E+308  

另请参阅

Explicit(XElement to Nullable<DateTime>)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 Nullable<T>DateTimeCast the value of this XElement to a Nullable<T> of DateTime.

public:
 static explicit operator Nullable<DateTime>(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator DateTime? (System.Xml.Linq.XElement element);
public static explicit operator DateTime? (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> Nullable<DateTime>
Public Shared Narrowing Operator CType (element As XElement) As Nullable(Of DateTime)

参数

element
XElement

要强制转换为 XElementNullable<T>DateTimeThe XElement to cast to Nullable<T> of DateTime.

返回

一个 Nullable<T>DateTime,其中包含此 XElement 的内容。A Nullable<T> of DateTime that contains the content of this XElement.

属性

例外

元素不是 null 且不包含有效的 DateTime 值。The element is not null and does not contain a valid DateTime value.

示例

下面的示例创建一个元素,该元素具有作为内容的日期和时间。The following example creates an element with a date and time as content. 然后,它通过强制转换为 DateTimeNullable<T> 来检索该值。It then retrieves the value by casting to Nullable<T> of DateTime.

XElement root = new XElement("Root",  
    new XElement("Value", new DateTime(2006, 10, 6, 12, 30, 0))  
);  
DateTime? value = (DateTime?)root.Element("Value");  
Console.WriteLine("Nullable DateTime: value={0}", value == null ? "null" : value.ToString());  
Dim root As XElement = _   
    <Root>  
        <Value><%= New DateTime(2006, 10, 6, 12, 30, 0) %></Value>  
    </Root>  
Dim value As Nullable(Of DateTime) = CType(root.Element("Value"), Nullable(Of DateTime))  
Console.WriteLine("Nullable DateTime: value={0}", IIf(value.HasValue, value.ToString(), "null"))  

该示例产生下面的输出:This example produces the following output:

Nullable DateTime: value=10/6/2006 12:30:00 PM  

注解

包含日期和时间内容的属性或元素的值空间与 ISO 8601 中所述的日期和时间密切相关。The value space of an attribute or element that contains date and time content is closely related to the dates and times described in ISO 8601. 当创建包含日期和时间内容的属性或元素时,将根据 W3C 规范设置属性或元素值的格式。When creating an attribute or element that contains date and time content, the attribute or element values are formatted per the W3C specification. 有关更多详细信息,请参阅 W3C 规范。See the W3C specification for more details.

从特性或元素强制转换为 DateTimeNullable<T> 时,此行为是不严格的。The behavior is lax when casting to a Nullable<T> of DateTime from an attribute or element. 即使属性或元素值的格式设置与 W3C 规范的格式完全不同,该值也会适当地转换为 DateTimeNullable<T>Even if the attribute or element value is not formatted exactly per the W3C specification, the value is appropriately converted to a Nullable<T> of DateTime.

此转换运算符使用 CultureInfo.InvariantCultureDateTime进行转换。This conversion operator uses CultureInfo.InvariantCulture to convert from a DateTime.

另请参阅

Explicit(XElement to Nullable<DateTimeOffset>)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 Nullable<T>DateTimeOffsetCast the value of this XElement to a Nullable<T> of DateTimeOffset.

public:
 static explicit operator Nullable<DateTimeOffset>(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator DateTimeOffset? (System.Xml.Linq.XElement element);
public static explicit operator DateTimeOffset? (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> Nullable<DateTimeOffset>
Public Shared Narrowing Operator CType (element As XElement) As Nullable(Of DateTimeOffset)

参数

element
XElement

强制转换为 XElement 类型的 Nullable<T>DateTimeOffsetThe XElement to cast to an Nullable<T> of DateTimeOffset.

返回

一个 Nullable<T>DateTimeOffset,其中包含此 XElement 的内容。A Nullable<T> of DateTimeOffset that contains the content of this XElement.

属性

例外

元素不是 null 且不包含有效的 DateTimeOffset 值。The element is not null and does not contain a valid DateTimeOffset value.

示例

下面的示例创建一个具有日期和时间内容的元素。The following example creates an element with date and time content. 然后,它将转换为 DateTimeOffsetNullable<T> 以检索值。It then casts to Nullable<T> of DateTimeOffset to retrieve the value.

XElement root = new XElement("Root",  
    new XElement("Child", new DateTimeOffset(new DateTime(2006, 10, 6, 12, 30, 0)))  
);  
Console.WriteLine(root);  
  
DateTimeOffset? dt = (DateTimeOffset?)root.Element("Child");  
Console.WriteLine("dt={0}", dt);  
Dim root As XElement = _  
    <Root>  
        <Child><%= New DateTimeOffset(New DateTime(2006, 10, 6, 12, 30, 0)) %></Child>  
    </Root>  
Console.WriteLine(root)  
  
Dim dt As Nullable(Of DateTimeOffset) = CType(root.<Child>(0), Nullable(Of DateTimeOffset))  
Console.WriteLine("dt={0}", dt)  

该示例产生下面的输出:This example produces the following output:

<Root>  
  <Child>2006-10-06T12:30:00-07:00</Child>  
</Root>  
dt=10/6/2006 12:30:00 PM -07:00  

注解

此转换运算符使用 XmlConvert 类来执行转换。This conversion operator uses the XmlConvert class to do the conversion.

另请参阅

Explicit(XElement to Int32)

重要

此 API 不符合 CLS。

将此 XElement 的值强制转换为 Int32Cast the value of this XElement to an Int32.

public:
 static explicit operator int(System::Xml::Linq::XElement ^ element);
[System.CLSCompliant(false)]
public static explicit operator int (System.Xml.Linq.XElement element);
public static explicit operator int (System.Xml.Linq.XElement element);
static member op_Explicit : System.Xml.Linq.XElement -> int
Public Shared Narrowing Operator CType (element As XElement) As Integer

参数

element
XElement

要强制转换为 XElementInt32The XElement to cast to Int32.

返回

一个 Int32,其中包含此 XElement 的内容。A Int32 that contains the content of this XElement.

属性

例外

元素不包含有效的 Int32 值。The element does not contain a valid Int32 value.

element 参数为 nullThe element parameter is null.

示例

下面的示例创建一个元素,其中包含整数作为内容。The following example creates an element with an integer as content. 然后,它通过强制转换为 Int32来检索值。It then retrieves the value by casting to Int32.

XElement root = new XElement("Root", 2147483647);  
int value = (int)root;  
Console.WriteLine("value={0}", value);  
Dim root As XElement = <Root>2147483647</Root>  
Dim value As Integer = CInt(root)  
Console.WriteLine("value={0}", value)  

该示例产生下面的输出:This example produces the following output:

value=2147483647  

另请参阅

适用于