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

保證Namespace XName物件的屬性不是 null。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

針對這個 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.

適用於

另請參閱