using System.Xml.Linq; XElement element = new XElement("root", new XElement("oldChild1"), new XElement("oldChild2")); XElement newChild1 = new XElement("newChild1"); XElement newChild2 = new XElement("newChild2"); element.ReplaceNodes(newChild1, newChild2); // Now the 'element' XElement contains the child elements 'newChild1' and 'newChild2'
using System.Xml.Linq; XElement element = new XElement("root", new XElement("oldChild1"), new XElement("oldChild2")); XText newText = new XText("This is the new content"); element.ReplaceNodes(newText); // Now the 'element' XElement contains only the text node 'This is the new content'Package library: The XElement class is part of the System.Xml.Linq namespace, which is included in the .NET Framework library.