共用方式為


在 C# 中預設命名空間的範圍 (LINQ to XML)

更新: November 2007

在 XML 樹狀結構中表示的預設命名空間不在查詢的範圍內。如果您擁有的 XML 位於預設命名空間中,您仍然必須宣告 XNamespace 變數,然後將它與區域名稱結合,讓限定名稱 (Qualified Name) 得以用於查詢中。

查詢 XML 時所遇到的其中一個最常見的問題是,如果 XML 樹狀結構有預設的命名空間,即使 XML 不在命名空間中,開發人員有時候還是會撰寫查詢。

本主題中的第一組範例會顯示載入預設命名空間中的 XML 但查詢錯誤的常見方式。

第二組範例顯示所需的修正,讓您可以在命名空間中查詢 XML。

如需詳細資訊,請參閱使用 XML 命名空間

範例

此範例顯示在命名空間中建立 XML,以及傳回空結果集的查詢。

程式碼

XElement root = XElement.Parse(
@"<Root xmlns='https://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='https://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

註解

此範例會產生下列結果:

Result set follows:
End of result set

範例

此範例顯示在命名空間中建立 XML,以及編碼正確的查詢。

相較於上述編碼錯誤的範例,使用 C# 時的正確方法為宣告與初始化 XNamespace 物件,並在指定 XName 物件時使用它。在這個情況下,Elements 方法的引數為 XName 物件。

使用 Visual Basic 時的正確方法為宣告並初始化全域預設命名空間。這會將所有 XML 屬性放在預設的命名空間中。此範例不需要其他任何修改,就可以讓它正常運作。

程式碼

XElement root = XElement.Parse(
@"<Root xmlns='https://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 = "https://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="https://www.adventure-works.com">

Module Module1
    Sub Main()
        Dim root As XElement = _
            <Root xmlns='https://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

註解

此範例會產生下列結果:

Result set follows:
1
2
3
End of result set

請參閱

概念

C# 中的命名空間 (LINQ to XML)