Cakupan namespace bawaan (LINQ ke XML)

Namespace bawaan seperti yang diwakili di pohon XML tidak termasuk dalam cakupan untuk kueri. Jika Anda memiliki XML yang berada di namespace default, Anda harus menggabungkan namespace dengan nama lokal untuk membuat nama berkualifikasi yang dapat digunakan dalam kueri.

Kesalahan umum dalam mengkueri pohon XML yang memiliki namespace default adalah menulis kueri seolah-olah XML tidak ada di namespace. Contoh pertama menunjukkan kueri yang umumnya tidak tepat dari namespace default. Yang kedua memperlihatkan kueri yang tepat.

Contoh: Kueri XML yang tidak tepat di namespace

Contoh ini memperlihatkan pembuatan XML di namespace, dan kueri yang tidak tepat yang mengembalikan set hasil kosong.

XElement root = XElement.Parse(
@"<Root xmlns='http://www.adventure-works.com'>
    <Child>1</Child>
    <Child>2</Child>
    <Child>3</Child>
    <AnotherChild>4</AnotherChild>
    <AnotherChild>5</AnotherChild>
    <AnotherChild>6</AnotherChild>
</Root>");
IEnumerable<XElement> c1 =
    from el in root.Elements("Child")
    select el;
Console.WriteLine("Result set follows:");
foreach (XElement el in c1)
    Console.WriteLine((int)el);
Console.WriteLine("End of result set");
Module Module1
    Sub Main()
        Dim root As XElement = _
            <Root xmlns='http://www.adventure-works.com'>
                <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
        Console.WriteLine("Result set follows:")
        For Each el As XElement In c1
            Console.WriteLine(CInt(el))
        Next
        Console.WriteLine("End of result set")
    End Sub
End Module

Contoh ini menghasilkan output berikut:

Result set follows:
End of result set

Contoh: Kueri XML yang tepat di namespace

Contoh ini memperlihatkan pembuatan XML di dalam sebuah namespace, dan kueri yang tepat.

Dalam C#, pendekatan yang benar adalah mendeklarasikan dan menginisialisasi XNamespace objek, dan menggunakannya saat menentukan XName objek. Dalam hal ini, argumen metode Elements adalah objek XName.

Pendekatan yang benar saat menggunakan Visual Basic adalah mendeklarasikan dan menginisialisasi namespace default global. Ini menempatkan semua properti XML di namespace default.

Berikut adalah kodenya:

XElement root = XElement.Parse(
@"<Root xmlns='http://www.adventure-works.com'>
    <Child>1</Child>
    <Child>2</Child>
    <Child>3</Child>
    <AnotherChild>4</AnotherChild>
    <AnotherChild>5</AnotherChild>
    <AnotherChild>6</AnotherChild>
</Root>");
XNamespace aw = "http://www.adventure-works.com";
IEnumerable<XElement> c1 =
    from el in root.Elements(aw + "Child")
    select el;
Console.WriteLine("Result set follows:");
foreach (XElement el in c1)
    Console.WriteLine((int)el);
Console.WriteLine("End of result set");
Imports <xmlns="http://www.adventure-works.com">

Module Module1
    Sub Main()
        Dim root As XElement = _
            <Root xmlns='http://www.adventure-works.com'>
                <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
        Console.WriteLine("Result set follows:")
        For Each el As XElement In c1
            Console.WriteLine(el.Value)
        Next
        Console.WriteLine("End of result set")
    End Sub
End Module

Contoh ini menghasilkan output berikut:

Result set follows:
1
2
3
End of result set

Lihat juga