Reputation: 303
I need to create this XML:
<?xml version="1.0" encoding="UTF-8"?>
<SRP xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="sample.xsd">
..but while I know how to write the namespace using XMLWriter (eg:
$XmlWriter.WriteAttributeString("xmlns", "xsi",
"http://www.w3.org/2000/xmlns/",
"http://www.w3.org/2001/XMLSchema-instance");
..I've been unable to do so in a variable:
[xml]$XML = New-Object System.Xml.XmlDocument
$Declaration=$XML.CreateXmlDeclaration("1.0","UTF-8",$Null)
$XML.AppendChild($Declaration)
I've tried various combinations of Namespacemanager but to no avail.. it seems a stupid question, but so far I've been unable to replicate it (short of using an here-string with the hardcoded XML and casting it as an XML of course, but I'd prefer to do it the 'proper' way :)
Thanks to everyone reading so far :)
Upvotes: 3
Views: 2496
Reputation: 9391
You don't need a namespace manager for this. Just create the nodes/attributes with the correct namespaces and let the XML processor figure out where to put them.
For your example:
$x = New-Object Xml.XmlDocument
$x.AppendChild($x.CreateXmlDeclaration("1.0","UTF-8",$null))
$node = $x.CreateElement("SRP")
$attr = $x.CreateAttribute("xsi","noNamespaceSchemaLocation","http://www.w3.org/2001/XMLSchema-instance")
$attr.Value="schema.xsd"
$node.SetAttributeNode($attr)
$x.AppendChild($node)
$x.OuterXml
Upvotes: 3