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");