## powershell -NoProfile -ExecutionPolicy Bypass -File xmlupdate.ps1 XMLファイル名 param( [Parameter(Mandatory = $true)] [string] $InputPath ) if (Test-Path $InputPath) { $dir = (Get-Location).Path $base = [System.IO.Path]::GetFileNameWithoutExtension($InputPath) $OutputPath = [System.IO.Path]::Combine($dir, ($base + ".new.xml")) } else { Write-Host "ファイルなし" exit } ## 整形用にインデントを指示 $settings = New-Object System.Xml.XmlWriterSettings $settings.Indent = $true ## XML読み込み $xmldoc = New-Object System.Xml.XmlDocument $xmldoc.Load($InputPath) ## ノード修正 $node = $xmldoc.SelectSingleNode("/root/items/item[@id='005RD']") if ($node -ne $null) { $node.InnerText="アイテムX" $node.SetAttribute("id", "005X") } ## ノード追加 $node = $xmldoc.SelectSingleNode("/root/items") if ($node -ne $null) { $itemdoc = $xmldoc.CreateElement("item") $itemdoc.InnerText="アイテム7Z" $itemdoc.SetAttribute("id","007Z") [void] $node.AppendChild($itemdoc) } ## 修正結果書き出し $writer = [System.Xml.XmlWriter]::Create($OutputPath, $settings) $xmldoc.Save($writer) $writer.Close()