Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

XAttribute Narrowing Conversion (XAttribute to TimeSpan)

Cast the value of this XAttribute to a TimeSpan.

This API is not CLS-compliant. 

Namespace:  System.Xml.Linq
Assemblies:   System.Xml.Linq (in System.Xml.Linq.dll)
  System.Xml.XDocument (in System.Xml.XDocument.dll)

Public Shared Narrowing Operator CType ( _
	attribute As XAttribute _
) As TimeSpan


Type: System.Xml.Linq.XAttribute

The XAttribute to cast to TimeSpan.

Return Value

Type: System.TimeSpan
A TimeSpan that contains the content of this XAttribute.


The attribute does not contain a valid TimeSpan value.


The attribute parameter is Nothing.

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.

The following example creates an attribute with time span content. It then retrieves the value by casting to TimeSpan.

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

This example produces the following output:


.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
© 2015 Microsoft