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 屬性會設定為 [None]。If 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.


針對這個 XNamespace 傳回 XName 的 URI。Returns the URI of the XNamespace for this XName.



判斷指定的 XName 與這個 XName 是否相等。Determines 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)

傳回擴充的 XML 名稱 (格式為 {namespace}localname)。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 是否等於指定的 XNameIndicates 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.