XElement Constructor (XElement)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Initializes a new instance of the XElement class from another XElement object.

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

Syntax

'Declaration
Public Sub New ( _
    other As XElement _
)
public XElement(
    XElement other
)

Parameters

Remarks

This constructor makes a deep copy of an element.

Examples

The following example creates an XML tree, creates a clone of the tree, and then calls DeepEquals, which tests whether the two XML trees are equal.

Dim output As New StringBuilder
Dim xmlTree As XElement = _
        <Root Att1="1">
            <Child1>1</Child1>
            <Child2>2</Child2>
        </Root>

' Create a clone of the tree.
Dim treeClone As XElement = New XElement(xmlTree)

output.Append(String.Format("xmlTree = treeClone: {0}", XNode.DeepEquals(xmlTree, treeClone)))
output.Append(Environment.NewLine)
' Do some work with xmlTree, perhaps pass it to other methods.
xmlTree.Add(New XElement("Child3", 3))

output.Append(String.Format("xmlTree = treeClone: {0}", XNode.DeepEquals(xmlTree, treeClone)))
output.Append(Environment.NewLine)

OutputTextBlock.Text = output.ToString()
StringBuilder output = new StringBuilder();
XElement xmlTree = new XElement("Root",
    new XAttribute("Att1", 1),
    new XElement("Child1", 1),
    new XElement("Child2", 2)
);

// Create a clone of the tree.
XElement treeClone = new XElement(xmlTree);

output.Append("xmlTree = treeClone: " + XNode.DeepEquals(xmlTree, treeClone) + Environment.NewLine);

// Do some work with xmlTree, perhaps pass it to other methods.
xmlTree.Add(new XElement("Child3", 3));

output.Append("xmlTree = treeClone: " + XNode.DeepEquals(xmlTree, treeClone) + Environment.NewLine);

OutputTextBlock.Text = output.ToString();

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.