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
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
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