XName Class (System.Xml.Linq)

Switch View :
ScriptFree
.NET Framework Class Library
XName Class

Represents a name of an XML element or attribute.

Inheritance Hierarchy

System.Object
  System.Xml.Linq.XName

Namespace:  System.Xml.Linq
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)
Syntax

Visual Basic
<SerializableAttribute> _
Public NotInheritable Class XName _
	Implements IEquatable(Of XName), ISerializable
C#
[SerializableAttribute]
public sealed class XName : IEquatable<XName>, 
	ISerializable
Visual C++
[SerializableAttribute]
public ref class XName sealed : IEquatable<XName^>, 
	ISerializable
F#
[<Sealed>]
[<SerializableAttribute>]
type XName =  
    class
        interface IEquatable<XName>
        interface ISerializable
    end

The XName type exposes the following members.

Properties

  Name Description
Public property Supported by the XNA Framework LocalName Gets the local (unqualified) part of the name.
Public property Supported by the XNA Framework Namespace Gets the namespace part of the fully qualified name.
Public property Supported by the XNA Framework NamespaceName Returns the URI of the XNamespace for this XName.
Top
Methods

  Name Description
Public method Supported by the XNA Framework Equals Determines whether the specified XName is equal to this XName. (Overrides Object.Equals(Object).)
Protected method Supported by the XNA Framework Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Static member Supported by the XNA Framework Get(String) Gets an XName object from an expanded name.
Public method Static member Supported by the XNA Framework Get(String, String) Gets an XName object from a local name and a namespace.
Public method Supported by the XNA Framework GetHashCode Gets a hash code for this XName. (Overrides Object.GetHashCode().)
Public method Supported by the XNA Framework GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method Supported by the XNA Framework MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Supported by the XNA Framework ToString Returns the expanded XML name in the format {namespace}localname. (Overrides Object.ToString().)
Top
Operators

  Name Description
Public operator Static member Supported by the XNA Framework Equality Returns a value indicating whether two instances of XName are equal.
Public operator Static member Supported by the XNA Framework Implicit(String to XName) Converts a string formatted as an expanded XML name (that is,{namespace}localname) to an XName object.
Public operator Static member Supported by the XNA Framework Inequality Returns a value indicating whether two instances of XName are not equal.
Top
Explicit Interface Implementations

  Name Description
Explicit interface implemetation Private method Supported by the XNA Framework IEquatable<XName>.Equals Indicates whether the current XName is equal to the specified XName.
Explicit interface implemetation Private method ISerializable.GetObjectData Populates a SerializationInfo with the data required to serialize the target object.
Top
Remarks

XML names include a namespace and a local name. A fully qualified name is the combination of the namespace and local name.

Creating an XName Object

XName does not contain any public constructors. Instead, this class provides an implicit conversion from String that allows you to create an 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:

C#
XElement root = new XElement("ElementName", "content");
Console.WriteLine(root);

In Visual Basic, it is more appropriate to use XML literals:

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

This example produces the following output:

xmlLang
<ElementName>content</ElementName>

Assigning a string to an XName uses the implicit conversion from String.

The Visual Basic example creates the XElement using XML literals. Even though XML literals are used, an XName object is created for the XElement.

In addition, you can call the Get method for an XName object. However, the recommended way is to use the implicit conversion from string.

Creating an XName in a Namespace

As with XML, an XName can be in a namespace, or it can be in no namespace.

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.

For Visual Basic, the recommended approach is to use XML literals and global namespace declarations to create XML that is in a namespace.

C#
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "ElementName", "content");
Console.WriteLine(root);
Visual Basic
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:

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

Creating an XName in no Namespace

The Namespace property of an XName object is guaranteed to not be null. If the XName is in no namespace, then the Namespace property will be set to None. The following code demonstrates this:

C#
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.");
Visual Basic
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

You can also create an XName from a expanded XML name in the form {namespace}localname:

C#
XElement root = new XElement("{http://www.adventure-works.com}ElementName", "content");
Console.WriteLine(root);
Visual Basic
Dim root As XElement = New XElement("{http://www.adventure-works.com}ElementName", "content")
Console.WriteLine(root)

This example produces the following output:

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

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. It is also less efficient than importing a global namespace and using XML literals in Visual Basic.

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. This additional time is likely to be negligible when writing LINQ queries; however, it might be significant when creating a large XML tree.

XName Objects are Atomized

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.

Version Information

.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also

Reference

Other Resources