using (XmlWriter writer = XmlWriter.Create("file.xml")) { writer.WriteStartElement("root"); writer.WriteElementString("element", "value"); writer.WriteEndElement(); } // Dispose method is automatically called
XmlWriterSettings settings = new XmlWriterSettings { Indent = true }; using (XmlWriter writer = XmlWriter.Create(Console.Out, settings)) { writer.WriteStartElement("root"); writer.WriteElementString("element", "value"); writer.WriteEndElement(); } // Dispose method is automatically calledIn this example, we create an XmlWriter instance that writes to the console with indentation enabled. The Dispose method is called automatically once the using block is exited. Package library: System.Xml