Export (0) Print
Expand All

XElement Constructor (XName, Object)

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

Namespace:  System.Xml.Linq
Assemblies:   System.Xml.XDocument (in System.Xml.XDocument.dll)
  System.Xml.Linq (in System.Xml.Linq.dll)

new : 
        name:XName * 
        content:Object -> XElement

Parameters

name
Type: System.Xml.Linq.XName

An XName that contains the element name.

content
Type: System.Object

The contents of the element.

This constructor creates an element with the specified content and 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.

For details about the valid content that can be passed to this constructor, see Valid Content of XElement and XDocument Objects.

The following example creates an XML tree. The content of the new element comes from a LINQ query.

No code example is currently available or this language may not be supported.

This example produces the following output:

<Root>
  <Child3>3</Child3>
  <Child4>4</Child4>
  <Child5>5</Child5>
</Root>

The following example creates an XML tree with a variety of types of content.

No code example is currently available or this language may not be supported.

This example produces the following output:

<Root>Some text</Root>
<Root>
  <NewChild>n</NewChild>
</Root>
<Root NewAttribute="n" />
<Root>12.345</Root>
<Root>2006-10-06T12:30:00</Root>
<Root>abcdefghi</Root>
<Root>
  <NewChild1>1</NewChild1>
  <NewChild2>2</NewChild2>
  <NewChild3>3</NewChild3>
</Root>
<Root NewAtt1="1" NewAtt2="2" NewAtt3="3" />

The following example creates an XML tree in a namespace.

No code example is currently available or this language may not be supported.

This example produces the following output:

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

The following example creates an XML tree with nested namespaces.

No code example is currently available or this language may not be supported.

This example produces the following output:

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft