XNamespace 类

定义

表示 XML 命名空间。Represents an XML namespace. 无法继承此类。This class cannot be inherited.

public ref class XNamespace sealed
public sealed class XNamespace
type XNamespace = class
Public NotInheritable Class XNamespace
继承
XNamespace

注解

此类表示命名空间的 XML 构造。This class represents the XML construct of namespaces.

每个 XName 都包含一个 XNamespaceEvery XName contains an XNamespace. 即使某个元素不在命名空间中,该元素的 XName 仍包含命名空间 XNamespace.NoneEven if an element is not in a namespace, the element's XName still contains a namespace, XNamespace.None. XName.Namespace 属性一定不会是 nullThe XName.Namespace property is guaranteed to not be null.

创建 XNamespace 对象Creating an XNamespace Object

创建 XNamespace 对象的最常见方法是只向其分配一个字符串。The most common way to create an XNamespace object is to simply assign a string to it. 然后,可以使用加法运算符的替代将命名空间与本地名称组合在一起。You can then combine the namespace with a local name by using the override of the addition operator. 下面的示例演示了这种用法:The following example shows this idiom:

XNamespace aw = "http://www.adventure-works.com";  
XElement root = new XElement(aw + "Root", "Content");  
Console.WriteLine(root);  
Dim aw As XNamespace = "http://www.adventure-works.com"  
Dim root As XElement = New XElement(aw + "Root", "Content")  
Console.WriteLine(root)  

但是,在 Visual Basic 中,通常会声明一个全局默认命名空间,如下所示:However, in Visual Basic, you would typically declare a global default namespace, as follows:

Imports <xmlns='http://www.adventure-works.com'>  
  
Module Module1  
    Sub Main()  
        Dim root As XElement = _  
            <Root>Content</Root>  
        Console.WriteLine(root)  
    End Sub  
End Module  

此示例生成以下输出:This example produces the following output:

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

将字符串分配给 XNamespace 使用来自 String的隐式转换。Assigning a string to an XNamespace uses the implicit conversion from String.

有关详细信息和示例,请参阅如何:C#使用命名空间创建文档()(LINQ to XML)See How to: Create a Document with Namespaces (C#) (LINQ to XML) for more information and examples.

有关使用 Visual Basic 中的命名空间的详细信息,请参阅Visual Basic (LINQ to XML)中的命名空间See Namespaces in Visual Basic (LINQ to XML) for more information on using namespaces in Visual Basic.

控制命名空间前缀Controlling Namespace Prefixes

如果创建声明命名空间的属性,则会在序列化的 XML 中保存在属性中指定的前缀。If you create an attribute that declares a namespace, the prefix specified in the attribute will be persisted in the serialized XML. 若要创建一个声明具有前缀的命名空间的属性,请创建一个属性,该属性的名称的命名空间为 Xmlns,该属性的名称为命名空间前缀。To create an attribute that declares a namespace with a prefix, you create an attribute where the namespace of the name of the attribute is Xmlns, and the name of the attribute is the namespace prefix. 该属性的值即是命名空间的 URI。The value of the attribute is the URI of the namespace. 下面的示例演示了这种用法:The following example shows this idiom:

XNamespace aw = "http://www.adventure-works.com";  
XElement root = new XElement(aw + "Root",  
    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),  
    "Content");  
Console.WriteLine(root);  
Dim aw As XNamespace = "http://www.adventure-works.com"  
Dim root As XElement = New XElement(aw + "Root", _  
    New XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"), _  
    "Content")  
Console.WriteLine(root)  

在 Visual Basic 中,你通常会使用全局命名空间声明,而不是创建命名空间节点来控制命名空间前缀:In Visual Basic, instead of creating a namespace node to control namespace prefixes, you would typically use a global namespace declaration:

Imports <xmlns:aw='http://www.adventure-works.com'>  
  
Module Module1  
    Sub Main()  
        Dim root As XElement = _  
            <aw:Root>Content</aw:Root>  
        Console.WriteLine(root)  
    End Sub  
End Module  

此示例生成以下输出:This example produces the following output:

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

有关详细信息,请参阅如何:控制命名空间前缀C#()(LINQ to XML)For more information, see How to: Control Namespace Prefixes (C#) (LINQ to XML).

创建默认命名空间Creating a Default Namespace

构造将作为命名空间的属性时,如果属性名称的特殊值为 "xmlns",则在序列化 XML 树时,会将命名空间声明为默认命名空间。When constructing an attribute that will be a namespace, if the attribute name has the special value of "xmlns", then when the XML tree is serialized, the namespace will be declared as the default namespace. 名称为 "xmlns" 本身的特殊属性不在任何命名空间中。The special attribute with the name of "xmlns" itself is not in any namespace. 该属性的值为命名空间 URI。The value of the attribute is the namespace URI.

下面的示例创建一个 XML 树,其中包含以命名空间将成为默认命名空间的方式声明的属性:The following example creates an XML tree that contains an attribute that is declared in such a way that the namespace will become the default namespace:

XNamespace aw = "http://www.adventure-works.com";  
XElement root = new XElement(aw + "Root",  
    new XAttribute("xmlns", "http://www.adventure-works.com"),  
    new XElement(aw + "Child", "content")  
);  
Console.WriteLine(root);  
Dim aw As XNamespace = "http://www.adventure-works.com"  
Dim root As XElement = New XElement(aw + "Root", _  
    New XAttribute("xmlns", "http://www.adventure-works.com"), _  
    New XElement(aw + "Child", "content") _  
)  
Console.WriteLine(root)  

在 Visual Basic 中,你通常会使用全局默认命名空间声明,而不是创建命名空间节点来创建默认命名空间:In Visual Basic, instead of creating a namespace node to create a default namespace, you would typically use a global default namespace declaration:

Imports <xmlns='http://www.adventure-works.com'>  
  
Module Module1  
    Sub Main()  
        Dim root As XElement = _  
            <Root>  
                <Child>content</Child>  
            </Root>  
        Console.WriteLine(root)  
    End Sub  
End Module  

此示例生成以下输出:This example produces the following output:

<Root xmlns="http://www.adventure-works.com">  
  <Child>content</Child>  
</Root>  

XNamespace 原子化XNamespace Atomization

保证 XNamespace 对象是原子化的;也就是说,如果两个 XNamespace 对象具有完全相同的 URI,则它们将共享同一个实例。XNamespace objects are guaranteed to be atomized; that is, if two XNamespace objects have exactly the same URI, they will share the same instance. 为此,显式提供了相等运算符和比较运算符。The equality and comparison operators are provided explicitly for this purpose.

使用展开名称Using Expanded Names

指定命名空间和本地名称的另一种方法是使用格式 {namespace}name的扩展名称:Another way to specify a namespace and a local name is to use an expanded name in the form {namespace}name:

XElement e = new XElement("{http://www.adventure-works.com}Root",  
     new XAttribute("{http://www.adventure-works.com}Att", "content")  
);  
Console.WriteLine(e);  
Dim e As XElement = New XElement("{http://www.adventure-works.com}Root", _  
     New XAttribute("{http://www.adventure-works.com}Att", "content") _  
)  
Console.WriteLine(e)  

此示例生成以下输出:This example produces the following output:

<Root p1:Att="content" xmlns:p1="http://www.adventure-works.com" xmlns="http://www.adventure-works.com" />  

这种方法的性能较低。This approach has performance implications. 每次将包含扩展名的字符串传递给 LINQ to XMLLINQ to XML 时,都必须分析名称,查找原子化命名空间,再查找原子化名称。Each time that you pass a string that contains an expanded name to LINQ to XMLLINQ to XML, it must parse the name, find the atomized namespace, and find the atomized name. 这个过程会占用 CPU 时间。This process takes CPU time. 如果性能很重要,则可能要使用不同的方法。If performance is important, you may want to use a different approach.

使用 Visual Basic 时,推荐的方法是使用 XML 文本,不涉及使用展开的名称。With Visual Basic, the recommended approach is to use XML literals, which does not involve the use of expanded names.

属性

NamespaceName

获取此命名空间的统一资源标识符 (URI)。Gets the Uniform Resource Identifier (URI) of this namespace.

None

获取不与命名空间对应的 XNamespace 对象。Gets the XNamespace object that corresponds to no namespace.

Xml

获取与 XML URI (http://www.w3.org/XML/1998/namespace) 对应的 XNamespace 对象。Gets the XNamespace object that corresponds to the XML URI (http://www.w3.org/XML/1998/namespace).

Xmlns

获取与 xmlns URI (http://www.w3.org/2000/xmlns/) 对应的 XNamespace 对象。Gets the XNamespace object that corresponds to the xmlns URI (http://www.w3.org/2000/xmlns/).

方法

Equals(Object)

确定指定的 XNamespace 是否等于当前 XNamespaceDetermines whether the specified XNamespace is equal to the current XNamespace.

Get(String)

获取指定统一资源标识符 (URI) 的 XNamespaceGets an XNamespace for the specified Uniform Resource Identifier (URI).

GetHashCode()

获取此 XNamespace 的哈希代码。Gets a hash code for this XNamespace.

GetName(String)

返回从此 XNamespace 和指定的本地名称创建的 XName 对象。Returns an XName object created from this XNamespace and the specified local name.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回此 XNamespace 的 URI。Returns the URI of this XNamespace.

运算符

Addition(XNamespace, String)

XName 对象与本地名称进行组合来创建 XNamespaceCombines an XNamespace object with a local name to create an XName.

Equality(XNamespace, XNamespace)

返回一个值,该值指示 XNamespace 的两个实例是否相等。Returns a value indicating whether two instances of XNamespace are equal.

Implicit(String to XNamespace)

将包含统一资源标识符 (URI) 的字符串转换为 XNamespaceConverts a string containing a Uniform Resource Identifier (URI) to an XNamespace.

Inequality(XNamespace, XNamespace)

返回一个值,该值指示 XNamespace 的两个实例是否相等。Returns a value indicating whether two instances of XNamespace are not equal.

适用于

另请参阅