Reputation:
I have this piece of xml code that I can trying to add to a file on my pc:
<Connector port="9089" protocol="org.apache.coyote.http11.Http11NioProtocol"/>
The code I have been trying to use:
$config = New-Object System.Xml.XmlDocument
$config.Load($filePath)
$newElement = $config.CreateElement('Connector')
$newElement.InnerXml = 'port="9089" protocol="org.apache.coyote.http11.Http11NioProtocol"'
$config.Server.Service.AppendChild($newElement)
$config.Save($filePath)
But instead of the desired output above, the code adds the following to the xml file:
<Connector>
port="9089" protocol="org.apache.coyote.http11.Http11NioProtocol"
</Connector>
What changes could I make to the code to get the above output rather than the below?
Upvotes: 2
Views: 56
Reputation: 58931
You want to add attributes so you have to use the SetAttribute
method:
$config = New-Object System.Xml.XmlDocument
$config.Load($filePath)
$newElement = $config.CreateElement('Connector')
$newElement.SetAttribute("port", "9089")
$newElement.SetAttribute("protocol", "org.apache.coyote.http11.Http11NioProtocol")
$config.Server.Service.AppendChild($newElement)
$config.Save($filePath)
Upvotes: 3