XAttribute.Value Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets or sets the value of this attribute.

Namespace:   System.Xml.Linq
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)

Public Property Value As String

Property Value

Type: System.String

A String containing the value of this attribute.

Exception Condition

When setting, the value is null.

You can use this property to get or set the value of an attribute.

Setting this property will raise the Changed and the Changing events.

If you are getting the value and the attribute might not exist, it is more convenient to use the explicit conversion operators, and assign the attribute to a nullable type such as string or Nullable(Of T) of Int32. If the attribute does not exist, then the nullable type is set to null. Before using this property, you must make sure that the Attribute method does not return null.

The following example creates an element with an attribute. It then retrieves the value of the attribute, and then sets it.

Note that the Visual Basic example uses the XML attribute property.

                Dim root As XElement = <Root Att="content"/>
root.@Att = "new text"

This example produces the following output:

new text

The following example shows the benefit of using the explicit conversion operators to get the value of an attribute that might not exist:

                Dim root As XElement = <Root Att1="attribute 1 content" Att2="2"/>

' The following assignments demonstrate why it is easier to use
' casting when the attribute might or might not exist.

Dim c1 As String = CStr(root.Attribute("Att1"))
Console.WriteLine("c1:{0}", IIf(c1 Is Nothing, "attribute does not exist", c1))

Dim c2 As Nullable(Of Integer) = CType(root.Attribute("Att2"), Nullable(Of Integer))
Console.WriteLine("c2:{0}", IIf(c2.HasValue, c2, "attribute does not exist"))

Dim c3 As String = CStr(root.Attribute("Att3"))
Console.WriteLine("c3:{0}", IIf(c3 Is Nothing, "attribute does not exist", c3))

Dim c4 As Nullable(Of Integer) = CType(root.Attribute("Att4"), Nullable(Of Integer))
Console.WriteLine("c4:{0}", IIf(c4.HasValue, c4, "attribute does not exist"))


' The following assignments show the necessary code when using
' the value property when the attribute might or might not exist.

Dim att1 As XAttribute = root.Attribute("Att1")
Dim v1 As String
If att1 Is Nothing Then
    v1 = Nothing
    v1 = att1.Value
End If

Console.WriteLine("v1:{0}", IIf(v1 Is Nothing, "attribute does not exist", v1))

Dim att2 As XAttribute = root.Attribute("Att2")
Dim v2 As Nullable(Of Integer)
If att2 Is Nothing Then
    v2 = Nothing
    v2 = Int32.Parse(att2.Value)
End If

Console.WriteLine("v2:{0}", IIf(v2.HasValue, v2, "attribute does not exist"))

Dim att3 As XAttribute = root.Attribute("Att3")
Dim v3 As String
If att3 Is Nothing Then
    v3 = Nothing
    v3 = att3.Value
End If

Console.WriteLine("v3:{0}", IIf(v3 Is Nothing, "attribute does not exist", v3))

Dim att4 As XAttribute = root.Attribute("Att4")
Dim v4 As Nullable(Of Integer)
If att4 Is Nothing Then
    v4 = Nothing
    v4 = Int32.Parse(att4.Value)
End If

Console.WriteLine("v4:{0}", IIf(v4.HasValue, v4, "attribute does not exist"))

This example produces the following output:

                c1:attribute 1 content
c3:attribute does not exist
c4:attribute does not exist

v1:attribute 1 content
v3:attribute does not exist
v4:attribute does not exist

Universal Windows Platform
Available since 8
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top