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

XElement Constructor (XName^, Object^)

 

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

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

public:
XElement(
	XName^ name,
	Object^ content
)

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>

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:
© 2016 Microsoft