Visual Basic enables you to specify an alias to a global XML namespace by using the Imports statement. The following example shows how to use the Imports statement to import an XML namespace:
Imports <xmlns:ns="http://someNamespace">
You can use an XML namespace alias when you access XML axis properties and declare XML literals for XML documents and elements.
You can retrieve an XNamespace object for a particular namespace prefix by using the GetXmlNamespace Operator.
For more information, see Imports Statement (XML Namespace).
Using XML Namespaces in XML Literals
The following example shows how to create an XElement object that uses the global namespace ns:
Dim contact1 As XElement = _
<ns:contact>
<ns:name>Patrick Hines</ns:name>
<ns:phone type="home">206-555-0144</ns:phone>
<ns:phone type="work">425-555-0145</ns:phone>
</ns:contact>
Console.WriteLine(contact1)
The Visual Basic compiler translates XML literals that contain XML namespace aliases into equivalent code that uses the XML notation for using XML namespaces, with the xmlns attribute. When compiled, the code in the previous section's example produces essentially the same executable code as the following example:
Dim contact2 As XElement = _
<ns1:contact xmlns:ns1="http://someNamespace">
<ns1:name>Patrick Hines</ns1:name>
<ns1:phone type="home">206-555-0144</ns1:phone>
<ns1:phone type="work">425-555-0145</ns1:phone>
</ns1:contact>
Console.WriteLine(contact2)
Using XML Namespaces in XML Axis Properties
XML namespaces declared in XML literals are not available for use in XML axis properties. However, global namespaces can be used with the XML axis properties. Use a colon to separate the XML namespace prefix from the local element name. Following is an example:
Console.WriteLine("Contact name is: " & contact1.<ns:name>.Value)