using System.Xml; XmlWriter writer = XmlWriter.Create("output.xml"); writer.WriteStartElement("person"); writer.WriteElementString("name", "John Doe"); writer.WriteEndElement(); writer.Close();
using System.Xml; XmlWriter writer = XmlWriter.Create("output.xml"); writer.WriteStartElement("books"); writer.WriteStartElement("book"); writer.WriteElementString("title", "The Lord of the Rings"); writer.WriteElementString("author", "J.R.R. Tolkien"); writer.WriteEndElement(); writer.WriteStartElement("book"); writer.WriteElementString("title", "Pride and Prejudice"); writer.WriteElementString("author", "Jane Austen"); writer.WriteEndElement(); writer.WriteEndElement(); writer.Close();This example creates an XML file called "output.xml" with a "books" element that has two child "book" elements, each with a "title" element and an "author" element. In both examples, the XmlWriter class is used to create the XML document, using the WriteStartElement method to indicate the beginning of an element, and the WriteEndElement method to indicate the end of an element. The WriteElementString method is used to write a complete XML element with a single string value. The XmlWriter.Create method is used to create an output stream for the XML document.