Mega Code Archive

 
Categories / C# Book / 05 LINQ XML
 

0537 XAttribute

To construct an XElement and XAttribute, simply provide a name and value: using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; class Program { static void Main() { XElement lastName = new XElement("name", "James"); lastName.Add(new XComment("It is a name")); XElement customer = new XElement("customer"); customer.Add(new XAttribute("id", 123)); customer.Add(new XElement("firstname", "Joe")); customer.Add(lastName); Console.WriteLine(customer.ToString()); } } The output: JoeJames