This documentation is archived and is not being maintained.

Namespace Handling in the XmlWriter

Namespaces are used to qualify element and attribute names in an XML document. Namespace prefixes associate elements and attributes with namespaces, which are in turn associated to a URI reference. Namespaces create element and attribute name uniqueness in an XML document.

The XmlWriter maintains a namespace stack corresponding to all the namespaces defined in the current namespace scope. When writing elements and attributes you can utilize namespaces in the following ways:

  • Declare namespaces manually.

  • Override the current namespace declaration with a new namespace.

  • Specify a namespace prefix when writing attributes or elements.

Namespace declarations can be written out manually using the WriteAttributeString method. This can be useful when you know how to best optimize the number of namespace declarations. In the following code a namespace declaration is created on the root element.

writer.WriteAttributeString("xmlns", "x", null, "urn:1");
writer.WriteStartElement("item", "urn:1");
writer.WriteStartElement("item", "urn:1");

The code generates the following XML string.

<root xmlns:x="urn:1">

Because the namespace declaration is on the root element, it is not duplicated on the two child elements. The item elements also pick up the prefix from the namespace declaration.

It is possible to manually override the namespace associated with a given prefix. In the following code, the namespace URI for the "x" prefix is changed from "123" to "abc".

writer.WriteStartElement("x", "root", "123");
writer.WriteAttributeString("xmlns", "x", null, "abc");

The code generates the following XML string:

<x:root xmlns:x="123">
  <item xmlns:x="abc" />

You can also pass the namespace prefix to use by explicitly specifying a prefix when writing an element or attribute. Many of the methods used to write element and attributes allow you to specify a namespace prefix. The following code shows how to specify a prefix when writing an element.

writer.WriteStartElement("x", "root", "urn:1");
writer.WriteStartElement("y", "item", "urn:1");

The code writes the following XML string:

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

The XmlWriter preserves both prefixes, "x" and "y".


All XmlWriter implementations, including XmlTextWriter, allow the namespace to be empty when a prefix is supplied. In this case, the writer ignores the prefix. This is a change in behavior in the Microsoft .NET Framework version 2.0 release. In previous versions, this condition results in an XmlException being thrown.