XNamespace.Addition Operator (XNamespace, String)

 
System_CAPS_noteNote

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

equivalentCodeEntity

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
)

Parameters

ns
Type: System.Xml.Linq.XNamespace

An XNamespace that contains the namespace.

localName
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")
);
Console.WriteLine(root);

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 = _
            <Root>
                <Child/>
            </Root>
        Console.WriteLine(root)
    End Sub
End Module

This example produces the following output:

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

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