Setting XML namespaces in XmlTextWriter correctly

Using [System.Xml.XmlTextWriter] in combination with [System.IO.StringWriter] you can easily create an arbitrary large XML document in memory on the fly. Creating and closing elements and attributes comes down to a simple pair of WriteStartelement()/WriteEndelement() and WriteAttributeString().
However when you want to create namespaces in your XML document things get a little bit trickier as the accompanying MSDN documentation for WriteAttributeString() is misleading.
It reads:

public void WriteAttributeString(
 string prefix,
 string localName,
 string ns,
 string value
)

… implying you could use a corresponding PowerShell statement like this:

 $XmlWriter.WriteAttributeString("xmlns", "xsi", $null, "http://www.w3.org/2001/XMLSchema-instance");

Exception calling "WriteAttributeString" with "4" argument(s): "The 'xmlns' attribute is bound to the reserved
namespace 'http://www.w3.org/2000/xmlns/'."
At line:1 char:1
+ $XmlWriter.WriteAttributeString("xmlns", "xsi", $null, "http://www.w3.org/2001/X ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
 + FullyQualifiedErrorId : ArgumentException 

Reading the error message you find, that you have to supply the ‘xmlns’ namespace as well instead of passing ‘$null’ (contradicting with the example code).

 $XmlWriter.WriteAttributeString("xmlns", "xsi", "http://www.w3.org/2000/xmlns/", 
  "http://www.w3.org/2001/XMLSchema-instance");

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: