C# 中的默认命名空间的范围 (LINQ to XML)

XML 树中表示的默认命名空间不在查询范围内。 如果您的 XML 在默认命名空间内,仍须声明一个 XNamespace 变量,并将该变量与本地名称组合在一起,生成一个限定名,在查询中使用。

查询 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)