我有一段XML代码,可以尝试将其添加到PC上的文件中:
<Connector port="9089" protocol="org.apache.coyote.http11.Http11NioProtocol"/>
我一直在尝试使用的代码:
$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)
但是,除了上面的期望输出之外,该代码还将以下内容添加到了xml文件中:
<Connector>
port="9089" protocol="org.apache.coyote.http11.Http11NioProtocol"
</Connector>
我可以对代码进行哪些更改以获取上面的输出而不是下面的输出?
您要添加属性,因此必须使用以下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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句