using System; using System.IO; using System.Xml; class Program { static void Main(string[] args) { // Create a new XmlTextWriter object XmlTextWriter writer = new XmlTextWriter("output.xml", null); // Start writing XML data writer.WriteStartDocument(); writer.WriteStartElement("root"); writer.WriteEndElement(); // Write the closing tags for the document writer.WriteEndDocument(); // Close the writer and file writer.Close(); } }
using System; using System.IO; using System.Xml; class Program { static void Main(string[] args) { // Create a new MemoryStream object MemoryStream stream = new MemoryStream(); // Create a new XmlTextWriter object XmlTextWriter writer = new XmlTextWriter(stream, null); // Start writing XML data writer.WriteStartDocument(); writer.WriteStartElement("root"); writer.WriteEndElement(); // Write the closing tags for the document writer.WriteEndDocument(); // Display the XML data Console.WriteLine(Encoding.UTF8.GetString(stream.ToArray())); // Close the writer and stream writer.Close(); stream.Close(); } }This example creates a new MemoryStream object and uses it as the output destination for the XmlTextWriter object. After writing the root element, the WriteEndDocument method is called to close the XML document. The XML data is then displayed in the console using the Encoding.UTF8.GetString method. Package library: System.Xml.