How to: Write Queries on XML in Namespaces
This page is specific to:.NET Framework Version:3.54.0
Language-Integrated Query (LINQ)
How to: Write Queries on XML in Namespaces

To write a query on XML that is in a namespace, you must use XName objects that have the correct namespace.

For C#, the most common approach is to initialize an XNamespace using a string that contains the URI, then use the addition operator overload to combine the namespace with the local name.

In Visual Basic, the most common approach is to define a global namespace, and then use XML literals and XML properties that use the global namespace. You can define a global default namespace, in which case elements in the XML literals will be in the namespace by default. Alternatively, you can define a global namespace with a prefix, and then use the prefix as required in the XML literals, and in XML properties. As with other forms of XML, attributes are always in no namespace by default.

The first set of examples in this topic shows how to create an XML tree in a default namespace in both C# and Visual Basic. The second set shows how to create an XML tree in a namespace with a prefix, also in both languages.

Example

The following example creates an XML tree that is in a default namespace. It then retrieves a collection of elements.

Imports <xmlns="http://www.adventure-works.com">

Module Module1
    Sub Main()
        Dim root As XElement = _
            <Root>
                <Child>1</Child>
                <Child>2</Child>
                <Child>3</Child>
                <AnotherChild>4</AnotherChild>
                <AnotherChild>5</AnotherChild>
                <AnotherChild>6</AnotherChild>
            </Root>
        Dim c1 As IEnumerable(Of XElement) = _
            From el In root.<Child> _
            Select el
        For Each el As XElement In c1
            Console.WriteLine(el.Value)
        Next
    End Sub
End Module

This example produces the following output:

1
2
3

In C#, you write queries in the same way regardless of whether you are writing queries on an XML tree that uses a namespace with a prefix or on an XML tree with a default namespace.

In Visual Basic, however, writing queries on an XML tree that uses a namespace with a prefix is quite different from querying an XML tree in a default namespace. Typically you use the Imports statement to import the namespace with a prefix. You then use the prefix in the element and attribute names when you construct the XML tree. You also use the prefix when querying an XML tree using XML properties.

The following example creates an XML tree that is in a namespace with a prefix. It then retrieves a collection of elements.

Imports <xmlns:aw="http://www.adventure-works.com">

Module Module1
    Sub Main()
        Dim root As XElement = _
            <aw:Root>
                <aw:Child>1</aw:Child>
                <aw:Child>2</aw:Child>
                <aw:Child>3</aw:Child>
                <aw:AnotherChild>4</aw:AnotherChild>
                <aw:AnotherChild>5</aw:AnotherChild>
                <aw:AnotherChild>6</aw:AnotherChild>
            </aw:Root>
        Dim c1 As IEnumerable(Of XElement) = _
            From el In root.<aw:Child> _
            Select el
        For Each el As XElement In c1
            Console.WriteLine(CInt(el))
        Next
    End Sub
End Module

This example produces the following output:

1
2
3
See Also

Other Resources

Community Content

You Have *GOT* To Be Kidding Me!
Added by:jhewett
Using an "Imports" to register a namespace into a module like you're doing is, quite frankly, a ludicrous hack. It's like begging someone to inject malware into your server-side code.

The RIGHT way to implement things in XDocument -- and what you SHOULD HAVE DONE IN THE FIRST PLACE -- is to COMPLETELY IGNORE any and all namespaces and other DTD / schema nonsense by default, and ONLY use the namespace(s), etc. if the PROGRAMMER ASKS FOR IT EXPLICITLY.

Hire some real programmers. Seriously.

Jerry H.

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View