XElement root = new XElement("root"); XElement child = new XElement("child", "text"); root.AddChild(child);
XElement parent = new XElement("parent"); for (int i = 1; i <= 5; i++) { XElement child = new XElement("child", i.ToString()); parent.AddChild(child); }In this example, we create a new XElement object named "parent" and then use a loop to create five child elements with the values of 1, 2, 3, 4, and 5. We then use the AddChild method to attach each child element as a child of the "parent" element. Package/library: System.Xml.Linq