XElement element = new XElement("Person", new XElement("Name", "John"), new XElement("Age", "25")); element.AddAttributeWithValue("Gender", "Male");
XElement bookElement = new XElement("Book", new XElement("Title", "The Catcher in the Rye"), new XElement("Author", "J.D. Salinger")); bookElement.AddAttributeWithValue("Price", "$9.99");In this example, we create an XML element called "Book" with child elements "Title" and "Author". We then use the AddAttributeWithValue method to add the attribute "Price" with a value of "$9.99" to the "Book" element. These examples use the System.Xml.Linq package library.