Mega Code Archive

 
Categories / C# Book / 05 LINQ XML
 

0535 Automatic Deep Cloning

you add an already parented node to a second parent, the node is automatically deep-cloned. using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; class Program { static void Main() { var address = new XElement("address",new XElement("street", "Main St"), new XElement("town", "New York")); var customer1 = new XElement("customer1", address); var customer2 = new XElement("customer2", address); customer1.Element("address").Element("street").Value = "Another St"; Console.WriteLine(customer2.Element("address").Element("street").Value); } } The output: Main St