HOW TO:在命名空間中撰寫 XML 的查詢

若要撰寫命名空間 (Namespace) 中的 XML 查詢,您必須使用具有正確命名空間的 XName 物件。

若為 C#,最常見的方法是使用包含 URI 的字串初始化 XNamespace,然後使用加法運算子多載結合命名空間與區域名稱。

在 Visual Basic 中,最常見的方法是定義全域命名空間,然後使用利用全域命名空間的 XML 常值和 XML 屬性。 您可以定義預設的全域命名空間,在此情況下,XML 常值中的項目預設會在命名空間中。 或者,您可以利用前置詞定義全域命名空間,然後在 XML 常值和 XML 屬性中使用前置詞 (如有需要)。 如同 XML 的其他格式,這些屬性預設永遠在沒有命名空間中。

本主題中的第一組範例會顯示如何同時在 C# 和 Visual Basic 中建立預設命名空間的 XML 樹狀結構。 第二組範例則顯示如何同時在這兩種語言中建立具有前置詞之命名空間的 XML 樹狀結構。

範例

下列範例會建立位於預設命名空間中的 XML 樹狀結構。 接著,它會擷取項目的集合。

XNamespace aw = "https://www.adventure-works.com";
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(aw + "Child")
    select el;
foreach (XElement el in c1)
    Console.WriteLine((int)el);
Imports <xmlns="https://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

此範例會產生下列輸出:

1
2
3

在 C# 中,不論您要針對使用具有前置詞之命名空間的 XML 樹狀結構,還是針對具有預設命名空間的 XML 樹狀結構撰寫查詢,查詢的撰寫方式都相同。

不過,在 Visual Basic 中,針對使用具有前置詞之命名空間的 XML 樹狀結構撰寫查詢與查詢預設命名空間中的 XML 樹狀結構相當不同。 一般而言,您會使用 Imports 陳述式來匯入具有前置詞的命名空間。 然後,當您建構 XML 樹狀結構時,就會將此前置詞用於項目和屬性名稱中。 此外,在使用 XML 屬性來查詢 XML 樹狀結構時,您也會使用此前置詞。

下列範例會建立位於命名空間中,具有前置詞的 XML 樹狀結構。 接著,它會擷取項目的集合。

XNamespace aw = "https://www.adventure-works.com";
XElement root = XElement.Parse(
@"<aw:Root xmlns:aw='https://www.adventure-works.com'>
    <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>");
IEnumerable<XElement> c1 =
    from el in root.Elements(aw + "Child")
    select el;
foreach (XElement el in c1)
    Console.WriteLine((int)el);
Imports <xmlns:aw="https://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

這個範例產生下列輸出:

1
2
3

請參閱

其他資源

使用 XML 命名空間