XElement.Explicit Operator

Definition

Overloads

Explicit(XElement to Nullable<Int64>)

Cast the value of this XElement to a Nullable<T> of Int64.

Explicit(XElement to Nullable<Single>)

Cast the value of this XElement to a Nullable<T> of Single.

Explicit(XElement to Nullable<TimeSpan>)

Cast the value of this XElement to a Nullable<T> of TimeSpan.

Explicit(XElement to Nullable<UInt32>)

Cast the value of this XElement to a Nullable<T> of UInt32.

Explicit(XElement to Nullable<UInt64>)

Cast the value of this XElement to a Nullable<T> of UInt64.

Explicit(XElement to UInt32)

Cast the value of this XElement to a UInt32.

Explicit(XElement to String)

Cast the value of this XElement to a String.

Explicit(XElement to TimeSpan)

Cast the value of this XElement to a TimeSpan.

Explicit(XElement to UInt64)

Cast the value of this XElement to a UInt64.

Explicit(XElement to Nullable<Int32>)

Cast the value of this XElement to a Nullable<T> of Int32.

Explicit(XElement to Single)

Cast the value of this XElement to a Single.

Explicit(XElement to Nullable<Guid>)

Cast the value of this XElement to a Nullable<T> of Guid.

Explicit(XElement to Nullable<Boolean>)

Cast the value of this XElement to a Nullable<T> of Boolean.

Explicit(XElement to Nullable<Decimal>)

Cast the value of this XElement to a Nullable<T> of Decimal.

Explicit(XElement to Boolean)

Cast the value of this XElement to a Boolean.

Explicit(XElement to DateTime)

Cast the value of this XElement to a DateTime.

Explicit(XElement to DateTimeOffset)

Cast the value of this XAttribute to a DateTimeOffset.

Explicit(XElement to Double)

Cast the value of this XElement to a Double.

Explicit(XElement to Guid)

Cast the value of this XElement to a Guid.

Explicit(XElement to Decimal)

Cast the value of this XElement to a Decimal.

Explicit(XElement to Int64)

Cast the value of this XElement to an Int64.

Explicit(XElement to Nullable<Double>)

Cast the value of this XElement to a Nullable<T> of Double.

Explicit(XElement to Nullable<DateTime>)

Cast the value of this XElement to a Nullable<T> of DateTime.

Explicit(XElement to Nullable<DateTimeOffset>)

Cast the value of this XElement to a Nullable<T> of DateTimeOffset.

Explicit(XElement to Int32)

Cast the value of this XElement to an Int32.

Explicit(XElement to Nullable<Int64>)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a Nullable<T> of Int64.

[System.CLSCompliant(false)]
public static explicit operator long?(System.Xml.Linq.XElement element);
[System.CLSCompliant(false)]
public static explicit operator long?(System.Xml.Linq.XElement? element);

Parameters

element
XElement

The XElement to cast to Nullable<T> of Int64.

Returns

A Nullable<T> of Int64 that contains the content of this XElement.

Attributes

Exceptions

The element is not null and does not contain a valid Int64 value.

Examples

The following example creates an element with long integer content. 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());

This example produces the following output:

Nullable ulong: value=9223372036854775807

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to Nullable<Single>)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a Nullable<T> of Single.

[System.CLSCompliant(false)]
public static explicit operator float?(System.Xml.Linq.XElement element);
[System.CLSCompliant(false)]
public static explicit operator float?(System.Xml.Linq.XElement? element);

Parameters

element
XElement

The XElement to cast to Nullable<T> of Single.

Returns

A Nullable<T> of Single that contains the content of this XElement.

Attributes

Exceptions

The element is not null and does not contain a valid Single value.

Examples

The following example creates an element with single precision floating point content. 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());

This example produces the following output:

Nullable Single: value=3.402823E+38

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to Nullable<TimeSpan>)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a Nullable<T> of TimeSpan.

[System.CLSCompliant(false)]
public static explicit operator TimeSpan?(System.Xml.Linq.XElement element);
[System.CLSCompliant(false)]
public static explicit operator TimeSpan?(System.Xml.Linq.XElement? element);

Parameters

element
XElement

The XElement to cast to Nullable<T> of TimeSpan.

Returns

A Nullable<T> of TimeSpan that contains the content of this XElement.

Attributes

Exceptions

The element is not null and does not contain a valid TimeSpan value.

Examples

The following example creates an element with time span content. 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());

This example produces the following output:

Nullable TimeSpan: value=01:05:30

Remarks

The value space of an attribute or element that contains time span content is closely related to duration content as described in ISO 8601. When creating an attribute or element that contains time span content, the attribute or element values are formatted per the W3C specification. Please see the W3C specification for more details.

Behavior is lax when casting to a Nullable<T> of TimeSpan from an attribute or element. 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.

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to Nullable<UInt32>)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a Nullable<T> of UInt32.

[System.CLSCompliant(false)]
public static explicit operator uint?(System.Xml.Linq.XElement element);
[System.CLSCompliant(false)]
public static explicit operator uint?(System.Xml.Linq.XElement? element);

Parameters

element
XElement

The XElement to cast to Nullable<T> of UInt32.

Returns

A Nullable<T> of UInt32 that contains the content of this XElement.

Attributes

Exceptions

The element is not null and does not contain a valid UInt32 value.

Examples

The following example creates an element with unsigned integer content. 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());

This example produces the following output:

Nullable uint: value=4294967295

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to Nullable<UInt64>)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a Nullable<T> of UInt64.

[System.CLSCompliant(false)]
public static explicit operator ulong?(System.Xml.Linq.XElement element);
[System.CLSCompliant(false)]
public static explicit operator ulong?(System.Xml.Linq.XElement? element);

Parameters

element
XElement

The XElement to cast to Nullable<T> of UInt64.

Returns

A Nullable<T> of UInt64 that contains the content of this XElement.

Attributes

Exceptions

The element is not null and does not contain a valid UInt64 value.

Examples

The following example creates an element with unsigned long integer content. 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());

This example produces the following output:

Nullable ulong: value=9223372036854775807

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to UInt32)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a UInt32.

[System.CLSCompliant(false)]
public static explicit operator uint(System.Xml.Linq.XElement element);

Parameters

element
XElement

The XElement to cast to UInt32.

Returns

A UInt32 that contains the content of this XElement.

Attributes

Exceptions

The element does not contain a valid UInt32 value.

The element parameter is null.

Examples

The following example creates an element with unsigned integer content. It then retrieves the value by casting to UInt32.

XElement root = new XElement("Root", 4294967295);
uint value = (uint)root;
Console.WriteLine("value={0}", value);

This example produces the following output:

value=4294967295

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to String)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a String.

[System.CLSCompliant(false)]
public static explicit operator string(System.Xml.Linq.XElement element);
[System.CLSCompliant(false)]
public static explicit operator string?(System.Xml.Linq.XElement? element);

Parameters

element
XElement

The XElement to cast to String.

Returns

A String that contains the content of this XElement.

Attributes

Examples

The following example creates an element with string content. 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);

This example produces the following output:

(string)root=abc def ghi

Remarks

If the XElement has children, the concatenated string value of all of the element's text and descendant's text is returned.

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to TimeSpan)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a TimeSpan.

[System.CLSCompliant(false)]
public static explicit operator TimeSpan(System.Xml.Linq.XElement element);

Parameters

element
XElement

The XElement to cast to TimeSpan.

Returns

A TimeSpan that contains the content of this XElement.

Attributes

Exceptions

The element does not contain a valid TimeSpan value.

The element parameter is null.

Examples

The following example creates an element with time span content. 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);

This example produces the following output:

value=01:05:30

Remarks

The value space of an attribute or element that contains time span content is closely related to duration content as described in ISO 8601. When creating an attribute or element that contains time span content, the attribute or element values are formatted per the W3C specification. Please see the W3C specification for more details.

Behavior is lax when casting to a TimeSpan from an attribute or element. Even if the attribute or element value is not formatted exactly per the W3C specification, the value is appropriately converted to a TimeSpan.

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to UInt64)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a UInt64.

[System.CLSCompliant(false)]
public static explicit operator ulong(System.Xml.Linq.XElement element);

Parameters

element
XElement

The XElement to cast to UInt64.

Returns

A UInt64 that contains the content of this XElement.

Attributes

Exceptions

The element does not contain a valid UInt64 value.

The element parameter is null.

element is null.

Examples

The following example creates an element with unsigned long integer content. It then retrieves the value by casting to UInt64.

XElement root = new XElement("Root", 18446744073709551615);
ulong value = (ulong)root;
Console.WriteLine("value={0}", value);

This example produces the following output:

value=18446744073709551615

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to Nullable<Int32>)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a Nullable<T> of Int32.

[System.CLSCompliant(false)]
public static explicit operator int?(System.Xml.Linq.XElement element);
[System.CLSCompliant(false)]
public static explicit operator int?(System.Xml.Linq.XElement? element);

Parameters

element
XElement

The XElement to cast to Nullable<T> of Int32.

Returns

A Nullable<T> of Int32 that contains the content of this XElement.

Attributes

Exceptions

The element is not null and does not contain a valid Int32 value.

Examples

The following example creates an element with unsigned integer content. 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());

This example produces the following output:

Nullable integer: value=2147483647

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to Single)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a Single.

[System.CLSCompliant(false)]
public static explicit operator float(System.Xml.Linq.XElement element);

Parameters

element
XElement

The XElement to cast to Single.

Returns

A Single that contains the content of this XElement.

Attributes

Exceptions

The element does not contain a valid Single value.

The element parameter is null.

element is null.

Examples

The following example creates an element with single precision floating point content. 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);

This example produces the following output:

value=3.402823E+38

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to Nullable<Guid>)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a Nullable<T> of Guid.

[System.CLSCompliant(false)]
public static explicit operator Guid?(System.Xml.Linq.XElement element);
[System.CLSCompliant(false)]
public static explicit operator Guid?(System.Xml.Linq.XElement? element);

Parameters

element
XElement

The XElement to cast to Nullable<T> of Guid.

Returns

A Nullable<T> of Guid that contains the content of this XElement.

Attributes

Exceptions

The element is not null and does not contain a valid Guid value.

Examples

The following example creates an element with guid content. 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());

This example produces the following output:

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

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to Nullable<Boolean>)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a Nullable<T> of Boolean.

[System.CLSCompliant(false)]
public static explicit operator bool?(System.Xml.Linq.XElement element);
[System.CLSCompliant(false)]
public static explicit operator bool?(System.Xml.Linq.XElement? element);

Parameters

element
XElement

The XElement to cast to Nullable<T> of Boolean.

Returns

A Nullable<T> of Boolean that contains the content of this XElement.

Attributes

Exceptions

The element is not null and does not contain a valid Boolean value.

Examples

The following example creates an element with boolean content. 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);

This example produces the following output:

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

Remarks

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.

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to Nullable<Decimal>)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a Nullable<T> of Decimal.

[System.CLSCompliant(false)]
public static explicit operator decimal?(System.Xml.Linq.XElement element);
[System.CLSCompliant(false)]
public static explicit operator decimal?(System.Xml.Linq.XElement? element);

Parameters

element
XElement

The XElement to cast to Nullable<T> of Decimal.

Returns

A Nullable<T> of Decimal that contains the content of this XElement.

Attributes

Exceptions

The element is not null and does not contain a valid Decimal value.

Examples

The following example creates an element with decimal content. 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());

This example produces the following output:

Nullable decimal: value=79228162514264337593543950335

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to Boolean)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a Boolean.

[System.CLSCompliant(false)]
public static explicit operator bool(System.Xml.Linq.XElement element);

Parameters

element
XElement

The XElement to cast to Boolean.

Returns

A Boolean that contains the content of this XElement.

Attributes

Exceptions

The element does not contain a valid Boolean value.

The element parameter is null.

Examples

The following example creates some elements with boolean values. It 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);

This example produces the following output:

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

Remarks

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.

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to DateTime)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a DateTime.

[System.CLSCompliant(false)]
public static explicit operator DateTime(System.Xml.Linq.XElement element);

Parameters

element
XElement

The XElement to cast to DateTime.

Returns

A DateTime that contains the content of this XElement.

Attributes

Exceptions

The element does not contain a valid DateTime value.

The element parameter is null.

element is null.

Examples

The following example creates an element with date and time content. 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);

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

Remarks

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. When creating an attribute or element that contains date and time content, the attribute or element values are formatted per the W3C specification. See the W3C specification for more details.

Behavior is lax when casting to a DateTime from an attribute or element. Even if the attribute or element value is not formatted exactly per the W3C specification, the value is appropriately converted to a DateTime.

This conversion operator uses CultureInfo.InvariantCulture to convert from a DateTime.

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to DateTimeOffset)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XAttribute to a DateTimeOffset.

[System.CLSCompliant(false)]
public static explicit operator DateTimeOffset(System.Xml.Linq.XElement element);

Parameters

element
XElement

The XElement to cast to DateTimeOffset.

Returns

A DateTimeOffset that contains the content of this XElement.

Attributes

Exceptions

The element does not contain a valid DateTimeOffset value.

The element parameter is null.

element is null.

Examples

The following example creates an element with date and time content. 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);

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

Remarks

This conversion operator uses the XmlConvert class to do the conversion.

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to Double)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a Double.

[System.CLSCompliant(false)]
public static explicit operator double(System.Xml.Linq.XElement element);

Parameters

element
XElement

The XElement to cast to Double.

Returns

A Double that contains the content of this XElement.

Attributes

Exceptions

The element does not contain a valid Double value.

The element parameter is null.

element is null.

Examples

The following example creates an attribute with double content. 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);

This example produces the following output:

value=1.79769313486231E+308

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to Guid)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a Guid.

[System.CLSCompliant(false)]
public static explicit operator Guid(System.Xml.Linq.XElement element);

Parameters

element
XElement

The XElement to cast to Guid.

Returns

A Guid that contains the content of this XElement.

Attributes

Exceptions

The element does not contain a valid Guid value.

The element parameter is null.

element is null.

Examples

The following example creates an element with a guid as content. 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);

This example produces the following output:

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

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to Decimal)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a Decimal.

[System.CLSCompliant(false)]
public static explicit operator decimal(System.Xml.Linq.XElement element);

Parameters

element
XElement

The XElement to cast to Decimal.

Returns

A Decimal that contains the content of this XElement.

Attributes

Exceptions

The element does not contain a valid Decimal value.

The element parameter is null.

element is null.

Examples

The following example creates an element with a decimal value. 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);

This example produces the following output:

value=79228162514264337593543950335

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to Int64)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to an Int64.

[System.CLSCompliant(false)]
public static explicit operator long(System.Xml.Linq.XElement element);

Parameters

element
XElement

The XElement to cast to Int64.

Returns

A Int64 that contains the content of this XElement.

Attributes

Exceptions

The element does not contain a valid Int64 value.

The element parameter is null.

element is null.

Examples

The following example creates an element with a long integer as content. 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);

This example produces the following output:

value=9223372036854775807

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to Nullable<Double>)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a Nullable<T> of Double.

[System.CLSCompliant(false)]
public static explicit operator double?(System.Xml.Linq.XElement element);
[System.CLSCompliant(false)]
public static explicit operator double?(System.Xml.Linq.XElement? element);

Parameters

element
XElement

The XElement to cast to Nullable<T> of Double.

Returns

A Nullable<T> of Double that contains the content of this XElement.

Attributes

Exceptions

The element is not null and does not contain a valid Double value.

Examples

The following example creates an element with double precision floating point content. 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());

This example produces the following output:

Nullable double: value=1.79769313486231E+308

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to Nullable<DateTime>)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a Nullable<T> of DateTime.

[System.CLSCompliant(false)]
public static explicit operator DateTime?(System.Xml.Linq.XElement element);
[System.CLSCompliant(false)]
public static explicit operator DateTime?(System.Xml.Linq.XElement? element);

Parameters

element
XElement

The XElement to cast to Nullable<T> of DateTime.

Returns

A Nullable<T> of DateTime that contains the content of this XElement.

Attributes

Exceptions

The element is not null and does not contain a valid DateTime value.

Examples

The following example creates an element with a date and time as content. 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());

This example produces the following output:

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

Remarks

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. When creating an attribute or element that contains date and time content, the attribute or element values are formatted per the W3C specification. See the W3C specification for more details.

The behavior is lax when casting to a Nullable<T> of DateTime from an attribute or element. 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.

This conversion operator uses CultureInfo.InvariantCulture to convert from a DateTime.

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to Nullable<DateTimeOffset>)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to a Nullable<T> of DateTimeOffset.

[System.CLSCompliant(false)]
public static explicit operator DateTimeOffset?(System.Xml.Linq.XElement element);
[System.CLSCompliant(false)]
public static explicit operator DateTimeOffset?(System.Xml.Linq.XElement? element);

Parameters

element
XElement

The XElement to cast to an Nullable<T> of DateTimeOffset.

Returns

A Nullable<T> of DateTimeOffset that contains the content of this XElement.

Attributes

Exceptions

The element is not null and does not contain a valid DateTimeOffset value.

Examples

The following example creates an element with date and time content. 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);

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

Remarks

This conversion operator uses the XmlConvert class to do the conversion.

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Explicit(XElement to Int32)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Important

This API is not CLS-compliant.

Cast the value of this XElement to an Int32.

[System.CLSCompliant(false)]
public static explicit operator int(System.Xml.Linq.XElement element);

Parameters

element
XElement

The XElement to cast to Int32.

Returns

A Int32 that contains the content of this XElement.

Attributes

Exceptions

The element does not contain a valid Int32 value.

The element parameter is null.

Examples

The following example creates an element with an integer as content. It then retrieves the value by casting to Int32.

XElement root = new XElement("Root", 2147483647);
int value = (int)root;
Console.WriteLine("value={0}", value);

This example produces the following output:

value=2147483647

See also

Applies to

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0