XDocument doc = new XDocument( new XComment("This is a comment."), new XProcessingInstruction("xml-stylesheet","href='mystyle.css' title='Compact' type='text/css'"), new XElement("Pubs", new XElement("Book", new XElement("Title", "Artifacts of Roman Civilization"), new XElement("Author", "Moreno, Jordao", new XAttribute("Name","Cds"), new XAttribute("ID","1")) ), new XElement("Book", new XElement("Title", "Midieval Tools and Implements"), new XElement("Author", "Gazit, Inbar") ), new XElement("Book", new XElement("Title","Asp.Net"), new XElement("Author","warrior") ) ), new XComment("This is a another comment.") ); doc.Declaration = new XDeclaration("1.0", "utf-8", "true"); if (System.IO.File.Exists("Sample.xml")) { System.IO.File.Delete("Sample.xml"); } doc.Save("Sample.xml");