XElement root = new XElement("Root", new XElement("Child", "child content") ); using (XmlWriter writer = root.CreateWriter()) { writer.WriteStartElement("Parent"); writer.WriteElementString("Name", "John"); writer.WriteEndElement(); writer.Flush(); }
XElement city = new XElement("City", new XElement("Name", "New York"), new XElement("Population", 8623000) ); using (XmlWriter writer = city.CreateWriter()) { city.Save(writer); }This example creates an XML document with a root element called "City" and two child elements called "Name" and "Population". The CreateWriter method is used to create a new instance of the XmlWriter class, and the Save method is called to write the XML to the stream. Overall, the System.Xml.Linq package library in C# provides a powerful set of tools for working with XML. The XElement CreateWriter method is just one example of the many useful features provided by this library.