XNamespace.Addition Operator (XNamespace, String)


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


Combines an XNamespace object with a local name to create an XName.

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

public static XName operator +(
	XNamespace ns,
	string localName


Type: System.Xml.Linq.XNamespace

An XNamespace that contains the namespace.

Type: System.String

A String that contains the local name.

Return Value

Type: System.Xml.Linq.XName

The new XName constructed from the namespace and local name.

This operator enables the common idiom of combining a namespace and a local name in the construction of an element or attribute. This idiom provides some of the benefits of having namespace prefixes, in that you can refer to a namespace using a variable that is short. This eliminates syntactic noise in the code that creates XML trees.

The following example shows the use of the + operator to create an XName from an XNamespace and a local name.

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

In Visual Basic, this is the preferred idiom:

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

Module Module1
    Sub Main()
        Dim aw As XNamespace = GetXmlNamespace()
        Dim root As XElement = _
    End Sub
End Module

This example produces the following output:

<Root xmlns="http://www.adventure-works.com">
  <Child />

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
Return to top