Remove elements, attributes, and nodes from an XML tree (LINQ to XML)

You can modify an XML tree, removing elements, attributes, and other types of nodes.

Removing a single element or a single attribute from an XML document is straightforward. However, when removing collections of elements or attributes, you should first materialize a collection into a list, and then delete the elements or attributes from the list. The best approach is to use the Remove extension method to do this.

The main reason to use this approach is that most of the collections you retrieve from an XML tree are yielded using deferred execution. If you don't first materialize them into a list, or if you don't use the extension methods, you may encounter a certain class of bugs. For more information, see Mixed declarative/imperative code bugs.

The following methods remove nodes and attributes from an XML tree.

Method Description
XAttribute.Remove Remove an XAttribute from its parent.
XContainer.RemoveNodes Remove the child nodes from an XContainer.
XElement.RemoveAll Remove content and attributes from an XElement.
XElement.RemoveAttributes Remove the attributes of an XElement.
XElement.SetAttributeValue Remove the attribute if you pass the value null.
XElement.SetElementValue Remove the child element if you pass the value null.
XNode.Remove Remove an XNode from its parent.
Extensions.Remove Remove every attribute or element in the source collection from its parent element.

Example: Remove a single element, and remove a collection of elements in two ways

This example demonstrates three approaches to removing elements. First, it removes a single element. Second, it retrieves a collection of elements, materializes them using the Enumerable.ToList operator, and then removes the collection. Finally, it retrieves a collection of elements and removes them using the Remove extension method.

For more information on the ToList operator, see Converting Data Types (C#) and Converting Data Types (Visual Basic).

XElement root = XElement.Parse(@"<Root>
    <Child1>
        <GrandChild1/>
        <GrandChild2/>
        <GrandChild3/>
    </Child1>
    <Child2>
        <GrandChild4/>
        <GrandChild5/>
        <GrandChild6/>
    </Child2>
    <Child3>
        <GrandChild7/>
        <GrandChild8/>
        <GrandChild9/>
    </Child3>
</Root>");
root.Element("Child1").Element("GrandChild1").Remove();
root.Element("Child2").Elements().ToList().Remove();
root.Element("Child3").Elements().Remove();
Console.WriteLine(root);
Dim root As XElement = _
    <Root>
        <Child1>
            <GrandChild1/>
            <GrandChild2/>
            <GrandChild3/>
        </Child1>
        <Child2>
            <GrandChild4/>
            <GrandChild5/>
            <GrandChild6/>
        </Child2>
        <Child3>
            <GrandChild7/>
            <GrandChild8/>
            <GrandChild9/>
        </Child3>
    </Root>
root.<Child1>.<GrandChild1>.Remove()
root.<Child2>.Elements().ToList().Remove()
root.<Child3>.Elements().Remove()
Console.WriteLine(root)

This example produces the following output:

<Root>
  <Child1>
    <GrandChild2 />
    <GrandChild3 />
  </Child1>
  <Child2 />
  <Child3 />
</Root>

The first grandchild element was removed from Child1, and all grandchildren elements were removed from Child2 and from Child3.