public WriteStartElement ( string prefix, string localName, string ns ) : void | ||
prefix | string | |
localName | string | |
ns | string | |
return | void |
using System.Xml; using System.IO; public class XmlWriterExample { public static void Main() { // Create XML writer with FileStream XmlTextWriter xmlWriter = new XmlTextWriter("example.xml", null); // Start root element xmlWriter.WriteStartElement("Data"); // Write child elements xmlWriter.WriteStartElement("Name"); xmlWriter.WriteString("John"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("Age"); xmlWriter.WriteString("30"); xmlWriter.WriteEndElement(); // End root element xmlWriter.WriteEndElement(); // Close the XML writer xmlWriter.Close(); } }
var stream = new MemoryStream(); var settings = new XmlWriterSettings() { Indent = true, IndentChars = "\t" }; var writer = XmlWriter.Create(stream, settings); writer.WriteStartElement("Root"); writer.WriteStartElement("Child"); writer.WriteAttributeString("Attribute", "Value"); writer.WriteString("Text"); writer.WriteEndElement(); writer.WriteEndElement(); writer.Flush(); stream.Position = 0; var reader = new StreamReader(stream); Console.WriteLine(reader.ReadToEnd());This example creates an XML writer with a MemoryStream and XmlWriterSettings that specify indentation settings. We then use the WriteStartElement method to create a "Root" element with a "Child" element inside that has an attribute and text value. We then flush the writer and read the stream to output the resulting XML.
public WriteStartElement ( string prefix, string localName, string ns ) : void | ||
prefix | string | |
localName | string | |
ns | string | |
return | void |