开发者

How can I update the value for a XML node using PowerShell? [duplicate]

This question already has answers here: Powershell script to update XML file content (3 answers) Closed 11 months ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜