XMLファイルを変更するPowerShellスクリプトを作成しています。私は新しい要素を追加し、その要素に属性を追加する必要があります。私はCreateElement()
とAppendChild()
メソッドを使ってみましたが、それは役に立たないです。以下は私のサンプル入力XMLファイルには、私は以下のように説明した後DeliveryServices
と呼ばれる新しいelemetntを追加し、その中Id
要素を追加する必要がXML文書の途中にXML要素を追加します。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Subnet xmlns="http://google.com">
<Id>Network_106</Id>
<Name>Network_106</Name>
<Description>
</Description>
<NetworkAddress>173.24.106.0</NetworkAddress>
<NetworkMask>255.255.255.0</NetworkMask>
</Subnet>
です。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Subnet xmlns="http://google.com">
<Id>Network_106</Id>
<Name>Network_106</Name>
<Description>
</Description>
<DeleveryServices>
<Id>172.22.22.22</Id>
</DeleveryServices>
<NetworkAddress>173.24.106.0</NetworkAddress>
<NetworkMask>255.255.255.0</NetworkMask>
</Subnet>
私は以下のコードを試しましたが、うまくいきませんでした。
[xml]$xdoc = Get-Content "F:\Sample.xml"
$child = $xdoc.CreateElement("DeleveryServices")
$xdoc.Subnet.AppendChild($child)
$xdoc.Subnet.DeleveryServices.Id = "172.22.22.22"
このエラーは以下のとおりです。また、DeleveryServices
要素がXMLファイルの最後に作成されます。私は説明の後にそれが欲しい。
The property 'Id' cannot be found on this object. Verify that the property exists and can be set. At line:44 char:17 + $xdoc.Subnet.DeleveryServices.Id = "172.22.22.22"
_「動作しません」_が不十分です。何が起こるかを説明し、実際の出力を表示し、エラーメッセージを含めます。 –
@ジムガリソンはエラーの詳細を追加しました。 –
XMLのどこにでもノードはありません。存在しないノードに属性を設定することはできません。 –