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.
object...<descendant>
A collection of XElement objects.
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 (Visual Basic).
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).
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
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