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转换, 它允许您XName创建。Instead, this class provides an implicit conversion from String that allows you to create an XName. 在构造元素或属性时, 使用此转换的最常见位置是:XElement构造函数的第一个参数XName是。The 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将使用中String的隐式转换。Assigning 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 XElement为创建一个对象。Even 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.

在命名空间中创建 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>  

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

XName对象Namespace的属性保证不为空。The Namespace property of an XName object is guaranteed to not be null. 如果不在命名空间中, Namespace则属性将设置为NoneXNameIf 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.

适用于

另请参阅