XName 类


表示 XML 元素或属性的名称。Represents a name of an XML element or attribute.

public ref class XName sealed : IEquatable<System::Xml::Linq::XName ^>, System::Runtime::Serialization::ISerializable
public sealed class XName : IEquatable<System.Xml.Linq.XName>, System.Runtime.Serialization.ISerializable
type XName = class
    interface IEquatable<XName>
    interface ISerializable
Public NotInheritable Class XName
Implements IEquatable(Of XName), ISerializable


XML 名称包括命名空间和本地名称。XML names include a namespace and a local name. 完全限定名是命名空间和本地名称的组合。A fully qualified name is the combination of the namespace and local name.

创建 XName 对象Creating an XName Object

XName 不包含任何公共构造函数。XName does not contain any public constructors. 相反,此类提供了从 String 的隐式转换,使您可以创建 XNameInstead, this class provides an implicit conversion from String that allows you to create an XName. 使用此转换的最常见位置是构造元素或属性: XElement 构造函数的第一个参数是 XNameThe most common place you use this conversion is when constructing an element or attribute: The first argument to the XElement constructor is an XName. 通过传递字符串,可以利用隐式转换。By passing a string, you take advantage of the implicit conversion. 下面的代码创建一个元素,该元素具有不在命名空间中的名称:The following code creates an element with a name that is in no namespace:

XElement root = new XElement("ElementName", "content");  

在 Visual Basic 中,更适合使用 XML 文本:In Visual Basic, it is more appropriate to use XML literals:

Dim root As XElement = <ElementName>content</ElementName>  

该示例产生下面的输出:This example produces the following output:


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

Visual Basic 示例使用 XML 文本创建 XElementThe Visual Basic example creates the XElement using XML literals. 即使使用 XML 文本,也会为 XElement创建 XName 对象。Even though XML literals are used, an XName object is created for the XElement.

此外,还可以对 XName 对象调用 Get 方法。In addition, you can call the Get method for an XName object. 不过,推荐的方法是使用字符串的隐式转换。However, the recommended way is to use the implicit conversion from string.

在命名空间中创建 XNameCreating an XName in a Namespace

与 XML 一样,XName 可以在命名空间中,也可以不在命名空间中。As with XML, an XName can be in a namespace, or it can be in no namespace.

对于C#,用于在命名空间中创建 XName 的建议方法是声明 XNamespace 对象,然后使用加法运算符的重写。For C#, the recommended approach for creating an XName in a namespace is to declare the XNamespace object, then use the override of the addition operator.

对于 Visual Basic,推荐的方法是使用 XML 文本和全局命名空间声明来创建位于命名空间中的 XML。For Visual Basic, the recommended approach is to use XML literals and global namespace declarations to create XML that is in a namespace.

XNamespace aw = "http://www.adventure-works.com";  
XElement root = new XElement(aw + "ElementName", "content");  
Imports <xmlns="http://www.adventure-works.com">  
Module Module1  
    Sub Main()  
        Dim root As XElement = <ElementName>content</ElementName>  
    End Sub  
End Module  

该示例产生下面的输出:This example produces the following output:

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

在无命名空间中创建 XNameCreating an XName in no Namespace

保证 XName 对象的 Namespace 属性不为 null。The Namespace property of an XName object is guaranteed to not be null. 如果 XName 不在命名空间中,则 Namespace 属性将设置为 NoneIf the XName is in no namespace, then the Namespace property will be set to None. 以下代码对此做了演示:The following code demonstrates this:

XElement root = new XElement("ElementName", "content");  
if (root.Name.Namespace == XNamespace.None)  
    Console.WriteLine("The element is in no namespace.");  
    Console.WriteLine("The element is in a namespace.");  
Dim root As XElement = <ElementName>content</ElementName>  
If (root.Name.Namespace Is XNamespace.None) Then  
    Console.WriteLine("The element is in no namespace.")  
    Console.WriteLine("The element is in a namespace.")  
End If  

该示例产生下面的输出:This example produces the following output:

The element is in no namespace.  

使用展开名称Using Expanded Names

你还可以 {namespace}localname格式从扩展的 XML 名称创建 XNameYou can also create an XName from a expanded XML name in the form {namespace}localname:

XElement root = new XElement("{http://www.adventure-works.com}ElementName", "content");  
Dim root As XElement = New XElement("{http://www.adventure-works.com}ElementName", "content")  

该示例产生下面的输出:This example produces the following output:

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

请注意,通过展开名称创建 XName 比创建 XNamespace 对象并使用加法运算符的替代更有效率。Be aware that creating an XName through an expanded name is less efficient than creating an XNamespace object and using the override of the addition operator. 与导入全局命名空间并在 Visual Basic 中使用 XML 文本相比,此操作的效率更低。It is also less efficient than importing a global namespace and using XML literals in Visual Basic.

如果使用展开名称创建 XName,LINQ to XML 必须找到命名空间的原子化实例。If you create an XName using an expanded name, LINQ to XML must find the atomized instance of a namespace. 对于每次使用展开名称,都必须重复此工作。This work must be repeated for every use of an expanded name. 编写 LINQ 查询时,此附加时间可能会忽略不计;但是,在创建大型 XML 树时,这可能很重要。This additional time is likely to be negligible when writing LINQ queries; however, it might be significant when creating a large XML tree.

XName 对象是原子化的XName Objects are Atomized

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

除了其他优点之外,此功能可以更快地执行查询。Among other benefits, this feature allows for faster execution of queries. 当对元素或属性的名称进行筛选时,谓词中表示的比较使用标识比较而不是值比较。When filtering on the name of elements or attributes, the comparisons expressed in predicates use identity comparison, not value comparison. 确定两个引用实际上引用相同的对象比比较两个字符串要快得多。It is much faster to determine that two references actually refer to the same object than to compare two strings.



获取名称的本地(非限定)部分。Gets the local (unqualified) part of the name.


获取完全限定名的命名空间部分。Gets the namespace part of the fully qualified name.


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



确定指定的 XName 是否等于此 XNameDetermines whether the specified XName is equal to this XName.


从展开名称获取 XName 对象。Gets an XName object from an expanded name.

Get(String, String)

从本地名称和命名空间获取 XName 对象。Gets an XName object from a local name and a namespace.


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


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

(继承自 Object)

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

(继承自 Object)

{namespace}localname 格式返回展开的 XML 名称。Returns the expanded XML name in the format {namespace}localname.


Equality(XName, XName)

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

Implicit(String to XName)

将已格式化为展开的 XML 名称(即 {namespace}localname)的字符串转换为 XName 对象。Converts a string formatted as an expanded XML name (that is,{namespace}localname) to an XName object.

Inequality(XName, XName)

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



表示当前的 XName 是否与指定的 XName 相等。Indicates whether the current XName is equal to the specified XName.

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

使用序列化目标对象时所需的数据填充 SerializationInfoPopulates a SerializationInfo with the data required to serialize the target object.