XmlTextWriter writer = new XmlTextWriter("output.xml", Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("rootElement"); writer.WriteWhitespace("\n\t"); writer.WriteStartElement("childElement"); writer.WriteString("Hello World"); writer.WriteEndElement(); writer.WriteEndElement(); writer.Close();
XmlTextWriter writer = new XmlTextWriter("output.xml", Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("rootElement"); writer.WriteAttributeString("attribute1", "value1"); writer.WriteWhitespace(" "); writer.WriteAttributeString("attribute2", "value2"); writer.WriteEndElement(); writer.Close();This code creates an XmlTextWriter object and sets its formatting to indented. It then begins writing the rootElement element and adds two attributes to it, separated by a space character. Finally, it ends the rootElement element and closes the writer. The resulting output file will have a space character between the two attributes, making it conform to typical XML formatting standards. Package/library: System.Xml.