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完整限定的名稱是命名空間和本機名稱的組合。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.

建立命名空間中的 XNameCreating an XName in a Namespace

如同 XMLXName可以位於命名空間,或也可以在沒有命名空間。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

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

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

方法

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

判斷指定的 XName 與這個 XName 是否相等。Determines 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 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

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

傳回擴充的 XML 名稱 (格式為 {namespace}localname)。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.

適用於

另請參閱