XML Child Axis Property (Visual Basic)

Provides access to the children of one of the following: an XElement object, an XDocument object, a collection of XElement objects, or a collection of XDocument objects.

Syntax

object.<child>

Parts

Term Definition
object Required. An XElement object, an XDocument object, a collection of XElement objects, or a collection of XDocument objects.
.< Required. Denotes the start of a child axis property.
child Required. Name of the child nodes to access, of the form [prefix:]name.

- Prefix - Optional. XML namespace prefix for the child node. Must be a global XML namespace defined with an Imports statement.
- Name - Required. Local child node name. See Names of Declared XML Elements and Attributes.
> Required. Denotes the end of a child axis property.

Return Value

A collection of XElement objects.

Remarks

You can use an XML child axis property to access child nodes by name from an XElement or XDocument object, or from a collection of XElement or XDocument objects. Use the XML Value property to access the value of the first child node in the returned collection. For more information, see XML Value Property.

The Visual Basic compiler converts child axis properties to calls to the Elements method.

XML Namespaces

The name in a child axis property can use only XML namespace prefixes declared globally with the Imports statement. It cannot use XML namespace prefixes declared locally within XML element literals. For more information, see Imports Statement (XML Namespace).

Example 1

The following example shows how to access the child nodes named phone from 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>

Dim homePhone = From hp In contact.<phone> 
                Where contact.<phone>.@type = "home" 
                Select hp

Console.WriteLine("Home Phone = {0}", homePhone(0).Value)

This code displays the following text:

Home Phone = 206-555-0144

Example 2

The following example shows how to access the child nodes named phone from the collection returned by the contact child axis property of the contacts object.

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

Dim homePhone = From contact In contacts.<contact> 
                Where contact.<phone>.@type = "home" 
                Select contact.<phone>

Console.WriteLine("Home Phone = {0}", homePhone(0).Value)

This code displays the following text:

Home Phone = 206-555-0144

Example 3

The following example declares ns as an XML namespace prefix. It then uses the prefix of the namespace to create an XML literal and access the first child node with the qualified name ns:name.

Imports <xmlns:ns = "http://SomeNamespace"> 
 
Class TestClass4

    Shared Sub TestPrefix()
        Dim contact = <ns:contact>
                        <ns:name>Patrick Hines</ns:name>
                      </ns:contact>
        Console.WriteLine(contact.<ns:name>.Value)
    End Sub

End Class

This code displays the following text:

Patrick Hines

See also