The following example imports 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 that has the qualified name ns:phone. It then passes that child node to the ShowName subroutine, which constructs a qualified name by using the GetXmlNamespace operator. The ShowName subroutine then passes the qualified name to the Ancestors method to get the parent ns:contact node.
' Place Imports statements at the top of your program.
Imports <xmlns:ns="http://SomeNamespace">
Module GetXmlNamespaceSample
Sub RunSample()
' Create test by using a global XML namespace prefix.
Dim contact = _
<ns:contact>
<ns:name>Patrick Hines</ns:name>
<ns:phone ns:type="home">206-555-0144</ns:phone>
<ns:phone ns:type="work">425-555-0145</ns:phone>
</ns:contact>
ShowName(contact.<ns:phone>(0))
End Sub
Sub ShowName(ByVal phone As XElement)
Dim qualifiedName = GetXmlNamespace(ns) + "contact"
Dim contact = phone.Ancestors(qualifiedName)(0)
Console.WriteLine("Name: " & contact.<ns:name>.Value)
End Sub
End Module
When you call TestGetXmlNamespace.RunSample(), it displays a message box that contains the following text:
Name: Patrick Hines