using System.Xml; using System.IO; XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; settings.Indent = true; using (XmlWriter writer = XmlWriter.Create("Output.xml", settings)) { writer.WriteStartElement("root"); writer.WriteStartElement("foo"); writer.WriteString("bar"); writer.WriteEndElement(); writer.WriteEndDocument(); }
using System.Xml; using System.IO; XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; settings.Indent = true; using (XmlWriter writer = XmlWriter.Create(Console.Out, settings)) { writer.WriteStartElement("root"); writer.WriteStartElement("foo"); writer.WriteString("bar"); writer.WriteEndElement(); writer.WriteEndDocument(); }In Example 1, an XML file is written using an XmlWriter. It starts with a root element, followed by a foo element that contains the word "bar". The WriteEndDocument method is used to close the XML document. In Example 2, instead of writing to a file, the XML is written to the console. The process is the same as Example 1, but with a different output destination. The System.Xml namespace is the package library for the XmlWriter class and the other classes and methods used in these examples.