Add Element to XML

为君一笑 提交于 2019-12-02 13:40:36

Your XML data uses namespaces, so you need to take care of that. The <core> node defines a default namespace (xmlns="urn:activemq:core") that applies to all of its child nodes. Create a namespace manager and add that namespace to it:

$nm = New-Object Xml.XmlNamespaceManager $xml.NameTable
$nm.AddNamespace('foo', 'urn:activemq:core')

Select the node to which you want to append your new node:

$cn = $xml.SelectSingleNode('//foo:connectors', $nm)

When creating the new node specify its default namespace, then set the node's attribute(s) and value:

$node = $xml.CreateElement('connector', $cn.NamespaceURI)
$node.SetAttribute('name', 'blaat')
$node.InnerText = 'tcp://xxxxx2:61616'

Now you can append the new node to the intended parent without getting a spurious xmlns attribute:

[void]$cn.AppendChild($node)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!