如何使用PowerShell向XML文件添加复杂元素?
我可以尝试将这段xml代码添加到电脑上的文件中:如何使用PowerShell向XML文件添加复杂元素?,xml,powershell,Xml,Powershell,我可以尝试将这段xml代码添加到电脑上的文件中: <Connector port="9089" protocol="org.apache.coyote.http11.Http11NioProtocol"/> 但是,代码将以下内容添加到xml文件中,而不是上面所需的输出: <Connector> port="9089" protocol="org.apache.coyote.http11.Http11NioProtocol" </Connector>
<Connector port="9089" protocol="org.apache.coyote.http11.Http11NioProtocol"/>
但是,代码将以下内容添加到xml文件中,而不是上面所需的输出:
<Connector>
port="9089" protocol="org.apache.coyote.http11.Http11NioProtocol"
</Connector>
port=“9089”protocol=“org.apache.coyote.http11.Http11NioProtocol”
我可以对代码进行哪些更改以获得上述输出而不是以下输出?您想要添加属性,因此必须使用SetAttribute
方法:
$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)
$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)