XElement Constructor (XName)
Collapse the table of content
Expand the table of content

XElement Constructor (XName)

 

Initializes a new instance of the XElement class with the specified name.

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

public XElement(
	XName name
)

Parameters

name
Type: System.Xml.Linq.XName

An XName that contains the name of the element.

This constructor creates an element with no content and no attributes.

There is an implicit conversion from string to XName. Typical use of this constructor is to specify a string as the parameter instead of creating a new XName. When creating an element in a namespace, typical use is to use the addition operator overload with an XNamespace and a string to create an XName. For more information, see Working with XML Namespaces.

The following example creates an element with no content.


                XElement el = new XElement("Root");
Console.WriteLine(el);

This example produces the following output:


<Root />

The following example creates an element in a namespace with no content. For more information, see Working with XML Namespaces.


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

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

Module Module1
    Sub Main()
        Dim root = <Root/>
        Console.WriteLine(root)
    End Sub
End Module

This example produces the following output:


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

Universal Windows Platform
Available since 4.5
.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:
© 2016 Microsoft