XName Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Represents a name of an XML element or attribute.

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


public sealed class XName : IEquatable<XName>, ISerializable


Gets the local (unqualified) part of the name.


Gets the namespace part of the fully qualified name.


Returns the URI of the XNamespace for this XName.


Determines whether the specified XName is equal to this XName.(Overrides Object.Equals(Object).)


Gets an XName object from an expanded name.

System_CAPS_pubmethodSystem_CAPS_staticGet(String, String)

Gets an XName object from a local name and a namespace.


Gets a hash code for this XName.(Overrides Object.GetHashCode().)


Gets the Type of the current instance.(Inherited from Object.)


Returns the expanded XML name in the format {namespace}localname.(Overrides Object.ToString().)

System_CAPS_puboperatorSystem_CAPS_staticEquality(XName, XName)

Returns a value indicating whether two instances of XName are equal.

System_CAPS_puboperatorSystem_CAPS_staticImplicit(String to XName)

Converts a string formatted as an expanded XML name (that is,{namespace}localname) to an XName object.

System_CAPS_puboperatorSystem_CAPS_staticInequality(XName, XName)

Returns a value indicating whether two instances of XName are not equal.


Indicates whether the current XName is equal to the specified XName.

System_CAPS_pubinterfaceSystem_CAPS_privmethodISerializable.GetObjectData(SerializationInfo, StreamingContext)

Populates a SerializationInfo with the data required to serialize the target object.

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

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:

                XElement root = new XElement("ElementName", "content");

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

                Dim root As XElement = <ElementName>content</ElementName>

This example produces the following output:


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.

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.

                XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "ElementName", "content");

This example produces the following output:

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

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:

                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.");

This example produces the following output:

The element is in no namespace.

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");

This example produces the following output:

<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 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.

Universal Windows Platform
Available since 8
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

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

Return to top