Removing Elements, Attributes, and Nodes from an XML Tree
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, which will do this for you.
The main reason for doing this is that most of the collections you retrieve from an XML tree are yielded using deferred execution. If you do not first materialize them into a list, or if you do not use the extension methods, it is possible to encounter a certain class of bugs. For more information, see Mixed Declarative Code/Imperative Code Bugs (LINQ to XML).
The following methods remove nodes and attributes from an XML tree.
Method | Description |
---|---|
System.Xml.Linq.XAttribute.Remove() |
Removes an XAttribute from its parent. |
System.Xml.Linq.XContainer.RemoveNodes() |
Removes the child nodes from an XContainer. |
Removes content and attributes from an XElement. |
|
Removes the attributes of an XElement. |
|
System.Xml.Linq.XElement.SetAttributeValue(System.Xml.Linq.XName,System.Object) |
If you pass null for value, then removes the attribute. |
System.Xml.Linq.XElement.SetElementValue(System.Xml.Linq.XName,System.Object) |
If you pass null for value, then removes the child element. |
Removes an XNode from its parent. |
|
Removes every attribute or element in the source collection from its parent element. |
Example
Description
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 System.Linq.Enumerable.ToList.Collections.Generic.IEnumerable{ operator, and 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.
Code
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)
Comments
This code produces the following output:
<Root>
<Child1>
<GrandChild2 />
<GrandChild3 />
</Child1>
<Child2 />
<Child3 />
</Root>
Notice that the first grandchild element has been removed from Child1
. All grandchildren elements have been removed from Child2
and from Child3
.