How to: Create a Document with Namespaces (LINQ to XML) (Visual Basic)

This topic shows how to create a document with namespaces in Visual Basic.

It is possible to use Visual Basic to create documents with namespaces by using the techniques described in How to: Create a Document with Namespaces (C#) (LINQ to XML). However, it is more convenient to use Visual Basic global namespace declarations.

When using XML literals in Visual Basic, users can define one global default XML namespace. This namespace is the default namespace for both XML literals and XML properties. The default XML namespace can be defined at either the project level or the file level. If it is defined at the file level, it overrides the default namespace at the project level.

You can also define other namespaces, and specify the namespace prefixes for those namespaces.

You define both default namespaces and namespaces with a prefix by using the Imports keyword.

For more information, see Introduction to XML Literals in Visual Basic.

Note that the default XML namespace only applies to elements and not to attributes. Attributes are by default always in no namespace. However, you can use a namespace prefix to put an attribute in a namespace.

Example

This example creates a document that contains a namespace.

Imports <xmlns:aw="https://www.adventure-works.com">
Module Module1
    Sub Main()
        Dim root As XElement = _
            <aw:Root>
                <aw:Child aw:Att="attvalue"/>
            </aw:Root>
        Console.WriteLine(root)
    End Sub
End Module

This example produces the following output:

<aw:Root xmlns:aw="https://www.adventure-works.com">
  <aw:Child aw:Att="attvalue" />
</aw:Root>

This example creates a document that contains two namespaces, one of which is the default namespace.

Imports <xmlns="https://www.adventure-works.com">
Imports <xmlns:fc="www.fourthcoffee.com">

Module Module1

    Sub Main()
        Dim root As XElement = _
            <Root>
                <Child Att="attvalue"/>
                <fc:Child2>child2 content</fc:Child2>
            </Root>
        Console.WriteLine(root)
    End Sub

End Module

This example produces the following output:

<Root xmlns:fc="www.fourthcoffee.com" xmlns="https://www.adventure-works.com">
  <Child Att="attvalue" />
  <fc:Child2>child2 content</fc:Child2>
</Root>

The following example creates a document that contains multiple namespaces, both with namespace prefixes.

When serializing an XML tree, LINQ to XML emits namespace declarations as required so that each element is in its designated namespace.

Imports <xmlns:aw="https://www.adventure-works.com">
Imports <xmlns:fc="www.fourthcoffee.com">

Module Module1

    Sub Main()
        Dim root As XElement = _
            <aw:Root>
                <fc:Child>
                    <aw:DifferentChild>other content</aw:DifferentChild>
                </fc:Child>
                <aw:Child2>c2 content</aw:Child2>
                <fc:Child3>c3 content</fc:Child3>
            </aw:Root>
        Console.WriteLine(root)
    End Sub

End Module

This example produces the following output:

<aw:Root xmlns:fc="www.fourthcoffee.com" xmlns:aw="https://www.adventure-works.com">
  <fc:Child>
    <aw:DifferentChild>other content</aw:DifferentChild>
  </fc:Child>
  <aw:Child2>c2 content</aw:Child2>
  <fc:Child3>c3 content</fc:Child3>
</aw:Root>

See Also

Concepts

Namespaces in Visual Basic (LINQ to XML)