How to: Access XML Descendant Elements (Visual Basic)

This example shows how to use a descendant axis property to access all XML elements that have a specified name and that are contained under an XML element. In particular, it uses the Value property to get the value of the first element in the collection that the name descendant axis property returns. The name descendant axis property gets all elements named name that are contained in the contacts object. This example also uses the phone descendant axis property to access all descendants named phone that are contained in the contacts object.

Example

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 phoneTypes As XElement = 
  <phoneTypes>
      <%= From phone In contacts...<phone> 
          Select <type><%= phone.@type %></type> 
      %>
  </phoneTypes>

Console.WriteLine(phoneTypes)

Compile the code

This example requires:

See also