Element Namespace Prefixes in the XmlTextWriter

You can pass, as a parameter to the WriteStartElement, the namespace prefix to prepend to the element. The method then prepends the namespace prefix to the element when it writes it out. The following code example shows the use of the namespace prefix in the WriteStartElement method call.

Dim w As New XmlTextWriter(Console.Out)
w.WriteStartElement("x", "root", "urn:1")
w.WriteStartElement("y", "item", "urn:1")
w.WriteEndElement()
w.WriteEndElement()
w.Close()
[C#]
XmlTextWriter w = new XmlTextWriter(Console.Out);
w.WriteStartElement("x","root","urn:1");
w.WriteStartElement("y","item","urn:1");
w.WriteEndElement();
w.WriteEndElement();
w.Close();

Output

<x:root xmlns:x="urn:1"><y:item xmlns:y="urn:1"/></x:root>

Both prefixes x and y have been preserved.

Note   Specifying a prefix and an empty namespace URI is an error that violates Section 2 of the W3C Namespace in XML specification (www.w3.org/TR/1999/REC-xml-names-19990114/ns-decl). If an empty namespace URI is given, an exception is thrown.

See Also

XmlTextWriter Class | XmlTextWriter Members | XmlWriter Class | XmlWriter Members