XML Value Property (Visual Basic)

Provides access to the value of the first element of a collection of XElement objects.

Syntax

object.Value

Parts

Term Definition
object Required. Collection of XElement objects.

Return Value

A String that contains the value of the first element of the collection, or Nothing if the collection is empty.

Remarks

The Value property makes it easy to access the value of the first element in a collection of XElement objects. This property first checks whether the collection contains at least one object. If the collection is empty, this property returns Nothing. Otherwise, this property returns the value of the Value property of the first element in the collection.

Note

When you access the value of an XML attribute using the '@' identifier, the attribute value is returned as a String and you do not need to explicitly specify the Value property.

To access other elements in a collection, you can use the XML extension indexer property. For more information, see Extension Indexer Property.

Inheritance

Most users will not have to implement IEnumerable<T>, and can therefore ignore this section.

The Value property is an extension property for types that implement IEnumerable(Of XElement). The binding of this extension property is like the binding of extension methods: if a type implements one of the interfaces and defines a property that has the name "Value", that property has precedence over the extension property. In other words, this Value property can be overridden by defining a new property in a class that implements IEnumerable(Of XElement).

Example 1

The following example shows how to use the Value property to access the first node in a collection of XElement objects. The example uses the child axis property to get the collection of all child nodes named phone that are in the contact object.

Dim contact As XElement = 
    <contact>
        <name>Patrick Hines</name>
        <phone type="home">206-555-0144</phone>
        <phone type="work">425-555-0145</phone>
    </contact>

Console.WriteLine("Phone number: " & contact.<phone>.Value)

This code displays the following text:

Phone number: 206-555-0144

Example 2

The following example shows how to get the value of an XML attribute from a collection of XAttribute objects. The example uses the attribute axis property to display the value of the type attribute for all of the phone elements.

Dim contact As XElement = 
    <contact>
      <name>Patrick Hines</name>
      <phone type="home">206-555-0144</phone>
      <phone type="work">425-555-0145</phone>
    </contact>


Dim types = contact.<phone>.Attributes("type")

For Each attr In types
  Console.WriteLine(attr.Value)
Next

This code displays the following text:

home
work

See also