Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PowerShell向XML文件添加复杂元素?_Xml_Powershell - Fatal编程技术网

如何使用PowerShell向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>

我可以尝试将这段xml代码添加到电脑上的文件中:

<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)