Rediger

Del via


XML Descendant Axis Property (Visual Basic)

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

Syntax

object...<descendant>

Parts

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 descendant axis property.

descendant Required. Name of the descendant nodes to access, of the form [prefix:]name.

Part Description
prefix Optional. XML namespace prefix for the descendant node. Must be a global XML namespace that is defined by using an Imports statement.
name Required. Local name of the descendant node. See Names of Declared XML Elements and Attributes.

> Required. Denotes the end of a descendant axis property.

Return Value

A collection of XElement objects.

Remarks

You can use an XML descendant axis property to access descendant 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 descendant node in the returned collection. For more information, see XML Value Property.

The Visual Basic compiler converts descendant axis properties into calls to the Descendants method.

XML Namespaces

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

Example 1

The following example shows how to access the value of the first descendant node named name and the values of all descendant nodes named phone from the contacts object.

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

Console.WriteLine("Name: " & contacts...<name>.Value)

Dim homePhone = From phone In contacts...<phone> 
                Select phone.Value

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

This code displays the following text:

Name: Patrick Hines

Home Phone = 206-555-0144

Example 2

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 value of the first child node with the qualified name ns:name.

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

    Shared Sub TestPrefix()
        Dim contacts = 
            <ns:contacts>
                <ns:contact>
                    <ns:name>Patrick Hines</ns:name>
                </ns:contact>
            </ns:contacts>

        Console.WriteLine("Name: " & contacts...<ns:name>.Value)
    End Sub

End Class

This code displays the following text:

Name: Patrick Hines

See also