XName XName XName XName Class

定义

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

public ref class XName sealed : IEquatable<System::Xml::Linq::XName ^>, System::Runtime::Serialization::ISerializable
[System.Runtime.Serialization.KnownType(typeof(System.Xml.Linq.NameSerializer))]
[System.Serializable]
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
继承
XNameXNameXNameXName
属性
实现

注解

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");  
Console.WriteLine(root);  

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

Dim root As XElement = <ElementName>content</ElementName>  
Console.WriteLine(root)  

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

<ElementName>content</ElementName>  

分配到一个字符串XName通过隐式转换从StringAssigning a string to an XName uses the implicit conversion from String.

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

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

在 Namespace 中创建一个 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");  
Console.WriteLine(root);  
Imports <xmlns="http://www.adventure-works.com">  

Module Module1  
    Sub Main()  
        Dim root As XElement = <ElementName>content</ElementName>  
        Console.WriteLine(root)  
    End Sub  
End Module  

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

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

在没有 Namespace 中创建一个 XNameCreating an XName in no Namespace

Namespace属性的XName保证对象不能为 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.");  
else  
    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.")  
Else  
    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

此外可以创建XName展开的 XML 名称的形式从{namespace}localname:You 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");  
Console.WriteLine(root);  
Dim root As XElement = New XElement("{http://www.adventure-works.com}ElementName", "content")  
Console.WriteLine(root)  

该示例产生下面的输出: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.

属性

LocalName LocalName LocalName LocalName

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

Namespace Namespace Namespace Namespace

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

NamespaceName NamespaceName NamespaceName NamespaceName

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

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

Get(String) Get(String) Get(String) Get(String)

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

Get(String, String) Get(String, String) Get(String, String) Get(String, String)

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

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

GetType() GetType() GetType() GetType()

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

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
ToString() ToString() ToString() ToString()

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

操作员

Equality(XName, XName) Equality(XName, XName) Equality(XName, XName) Equality(XName, XName)

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

Implicit(String to XName) Implicit(String to XName) Implicit(String to XName) 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) Inequality(XName, XName) Inequality(XName, XName) Inequality(XName, XName)

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

显式界面实现

IEquatable<XName>.Equals(XName) IEquatable<XName>.Equals(XName) IEquatable<XName>.Equals(XName) IEquatable<XName>.Equals(XName)

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

ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext)

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

适用于

另请参阅