- prefix
- The namespace prefix of the element.
- prefix
- The namespace prefix of the element.
- localName
- The local name of the element.
- ns
- The namespace URI to associate with the element. If this namespace is already in scope and has an associated prefix then the writer automatically writes that prefix also.
Type Reason ArgumentException XmlTextWriter.Namespaces is false for the writer, and prefix and ns are not both null. InvalidOperationException The XmlTextWriter.WriteState is WriteState.Closed.
In the dnprdnext release, the recommended practice is to create System.Xml.XmlWriter instances using the erload:System.Xml.XmlWriter.Create method and the System.Xml.XmlWriterSettings class. This allows you to take full advantage of all the new features introduced in this release. For more information, see Creating XML Writers.
After calling this method you can either write attributes or create content using XmlTextWriter.WriteComment(string), XmlTextWriter.WriteString(string), or XmlTextWriter.WriteStartElement(string, string, string) for child elements. You can close the element with XmlTextWriter.WriteEndElement at which time an end tag is written out.
This example demonstrates the XmlTextWriter.WriteStartElement(string, string, string) method, writing the XML to the console.
C# Example
using System; using System.Xml; public class WriteXml { public static void Main() { XmlTextWriter xWriter = new XmlTextWriter(Console.Out); xWriter.WriteStartDocument(); xWriter.WriteStartElement("prefix","element", "namespace"); xWriter.WriteEndDocument(); } }
The output is
<?xml version="1.0" encoding= "someencoding"?>
<prefix:element xmlns:prefix="namespace" />
The value of the encoding attribute is the encoding of the output stream of the console.