XElement root = new XElement("root", new XElement("child1", "value1"), new XElement("child2", "value2")); root.Element("child2").AddBeforeSelf(new XElement("newChild", "newValue")); Console.WriteLine(root);
value1 newValue value2
XElement product = new XElement("product", new XElement("name", "iPhone"), new XElement("price", "$999")); product.Element("price").AddBeforeSelf(new XElement("discount", "10%")); Console.WriteLine(product);
Overall, XElement AddBeforeSelf is a useful method for manipulating XML data in a C# program using the System.Xml.Linq package.iPhone 10% $999