XmlWriter writer = XmlWriter.Create("example.xml"); writer.WriteStartElement("root"); writer.WriteElementString("child", "value"); writer.WriteEndElement(); writer.Close();
XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; XmlWriter writer = XmlWriter.Create("example.xml", settings); writer.WriteStartDocument(); writer.WriteStartElement("root"); writer.WriteStartElement("child"); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Close();In this example, we create an XmlWriter with formatting settings to enable indentation. We then write the XML declaration with WriteStartDocument, and a root element. We then write a child element and close it with WriteEndElement. Finally, we close the root element and the XmlWriter with WriteEndElement and WriteEndDocument respectively. The System.Xml package library provides the XmlWriter class.