2

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?

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.