How can I update the value for a XML node using PowerShell? [duplicate]
How can I change the value of the node
<Test>Test</Test>
to
<Test>Power</Test>
?
Example:
<?xml version="1.0"?>
<configuration>
<appSettings>
<开发者_StackOverflow社区;add key="DeploymentDate" value="test" />
<add key="DateOfInitialization" value="Vinoj" />
</appSettings>
<Test>Test</Test>
</configuration>
Here's the PowerShell script I currently use:
$configuration = "app.config"
[xml]$xml = New-Object XML
$xml.Load($configuration)
$xml.selectnodes("/configuration/Test") = {"UST"}
$xml.Save($configuration)
I don't know what exactly you want to achieve, but the example should give you and idea:
$file = 'c:\temp\aa\ServerService.exe.config'
$x = [xml] (Get-Content $file)
Select-Xml -xml $x -XPath //root/level |
% { $_.Node.'#text' = 'test'
$_.Node.SomeAttribute = 'value'
}
$x.Save($file)
You don't need to use .NET for xpath queries. Just stay with PowerShell (with Select-Xml
).
It is also common to load xml file via Get-Content
and cast it to [xml]
which creates XmlDocument
and loads the file content.
精彩评论